// Fiscal Period Types for Danish Accounting (Regnskabsperioder)

/**
 * Period frequency - how often accounting periods are defined
 */
export type PeriodFrequency =
  | 'monthly'      // Maanedlig
  | 'quarterly'    // Kvartalsvis
  | 'half-yearly'  // Halvaarlig
  | 'yearly';      // Aarlig

/**
 * Period status according to Danish accounting requirements
 */
export type PeriodStatus =
  | 'future'      // Fremtidig - not yet started
  | 'open'        // Aaben - current working period
  | 'closed'      // Lukket - closed but can be reopened
  | 'locked';     // Laast - permanently locked (after arsafslutning)

/**
 * VAT Period frequency (can differ from accounting periods)
 * Based on SKAT requirements
 */
export type VATPeriodicitet =
  | 'monthly'      // Maanedlig (omsaetning > 50M DKK)
  | 'quarterly'    // Kvartalsvis (default for most)
  | 'half-yearly'  // Halvaarlig (omsaetning < 1M DKK, optional)
  | 'yearly';      // Aarlig (omsaetning < 300K DKK, optional)

/**
 * Fiscal Year (Regnskabsaar)
 */
export interface FiscalYear {
  id: string;
  companyId: string;

  // Year identification
  name: string;              // e.g., "2024/2025" or "2025"
  startDate: string;         // ISO date
  endDate: string;           // ISO date

  // Status
  status: 'open' | 'closed' | 'locked';

  // Year-end closing
  closingDate?: string;      // When year-end was performed
  closedBy?: string;         // User who closed the year
  openingBalancePosted: boolean;

  // Metadata
  createdAt: string;
  updatedAt: string;
}

/**
 * Accounting Period (Regnskabsperiode)
 */
export interface AccountingPeriod {
  id: string;
  companyId: string;
  fiscalYearId: string;

  // Period identification
  periodNumber: number;       // 1-12 for monthly, 1-4 for quarterly, etc.
  name: string;               // e.g., "Januar 2025" or "Q1 2025"
  shortName: string;          // e.g., "Jan 2025" or "Q1"

  // Date range
  startDate: string;          // ISO date
  endDate: string;            // ISO date

  // Status
  status: PeriodStatus;

  // Status change tracking
  closedAt?: string;
  closedBy?: string;
  lockedAt?: string;
  lockedBy?: string;
  reopenedAt?: string;
  reopenedBy?: string;

  // Metadata
  createdAt: string;
  updatedAt: string;
}

/**
 * VAT Period (Momsperiode) for SKAT reporting
 */
export interface VATPeriod {
  id: string;
  companyId: string;

  // Period identification
  periodicitet: VATPeriodicitet;
  year: number;
  periodNumber: number;       // 1-12 for monthly, 1-4 for quarterly, etc.
  name: string;               // e.g., "Q4 2024"

  // Date range
  startDate: string;
  endDate: string;

  // SKAT deadlines
  deadline: string;           // Frist for indberetning
  paymentDeadline?: string;   // Frist for betaling

  // Status
  status: 'future' | 'open' | 'closed' | 'draft' | 'submitted' | 'accepted' | 'rejected';
  submittedAt?: string;
  submissionReference?: string; // SKAT reference number

  // Calculated amounts (from transactions)
  netVAT?: number;            // Moms til betaling/tilgode

  // Metadata
  createdAt: string;
  updatedAt: string;
}

/**
 * Period Settings per Company
 */
export interface PeriodSettings {
  companyId: string;

  // Accounting periods
  accountingFrequency: PeriodFrequency;
  fiscalYearStartMonth: number; // 1-12

  // VAT periods
  vatFrequency: VATPeriodicitet;

  // Auto-close settings
  autoClosePeriods: boolean;
  autoCloseDelayDays: number;  // Days after period end to auto-close

  // Validation settings
  preventPostingToClosedPeriods: boolean;
  preventPostingToFuturePeriods: boolean;
  requirePeriodCloseApproval: boolean;
}

/**
 * Period Context - for components that need period awareness
 */
export interface PeriodContext {
  currentFiscalYear: FiscalYear | null;
  currentPeriod: AccountingPeriod | null;
  selectedPeriod: AccountingPeriod | null;
  selectedVATPeriod: VATPeriod | null;

  // Comparison period for reports
  comparisonPeriod?: AccountingPeriod | null;
  comparisonType: 'previous-period' | 'previous-year' | 'custom' | null;
}

/**
 * Year-end closing entry types
 */
export type ClosingEntryType =
  | 'revenue-close'      // Close revenue accounts to result
  | 'expense-close'      // Close expense accounts to result
  | 'result-transfer'    // Transfer result to equity
  | 'opening-balance';   // Opening balances for new year

/**
 * Year-end closing entry
 */
export interface ClosingEntry {
  id: string;
  fiscalYearId: string;
  type: ClosingEntryType;
  transactionId: string;     // Reference to created transaction
  amount: number;
  description: string;
  createdAt: string;
}

/**
 * Period validation result
 */
export interface PeriodValidationResult {
  isValid: boolean;
  canPost: boolean;
  errors: PeriodValidationError[];
  warnings: PeriodValidationWarning[];
}

export interface PeriodValidationError {
  code: string;
  message: string;
  messageDanish: string;
  field?: string;
}

export interface PeriodValidationWarning {
  code: string;
  message: string;
  messageDanish: string;
}

/**
 * Danish month names for display
 */
export const DANISH_MONTHS = [
  'Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni',
  'Juli', 'August', 'September', 'Oktober', 'November', 'December'
] as const;

/**
 * Danish short month names
 */
export const DANISH_MONTHS_SHORT = [
  'Jan', 'Feb', 'Mar', 'Apr', 'Maj', 'Jun',
  'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'
] as const;

/**
 * Period frequency display names
 */
export const PERIOD_FREQUENCY_NAMES: Record<PeriodFrequency, { danish: string; english: string }> = {
  'monthly': { danish: 'Maanedlig', english: 'Monthly' },
  'quarterly': { danish: 'Kvartalsvis', english: 'Quarterly' },
  'half-yearly': { danish: 'Halvaarslig', english: 'Half-yearly' },
  'yearly': { danish: 'Aarlig', english: 'Yearly' },
};

/**
 * Period status display names and colors
 */
export const PERIOD_STATUS_CONFIG: Record<PeriodStatus, {
  danish: string;
  english: string;
  color: string;
  icon: string;
}> = {
  'future': {
    danish: 'Fremtidig',
    english: 'Future',
    color: 'default',
    icon: 'clock-circle'
  },
  'open': {
    danish: 'Aaben',
    english: 'Open',
    color: 'green',
    icon: 'check-circle'
  },
  'closed': {
    danish: 'Lukket',
    english: 'Closed',
    color: 'orange',
    icon: 'minus-circle'
  },
  'locked': {
    danish: 'Laast',
    english: 'Locked',
    color: 'red',
    icon: 'lock'
  },
};
