import type {
  FiscalYear,
  AccountingPeriod,
  JournalEntry,
  JournalLine,
  PeriodClose,
  TrialBalance,
  TrialBalanceLine,
  FinancialStatement,
  FinancialStatementLineItem,
  Schema,
} from "../lib/types";

const createdAt = new Date("2024-01-01T00:00:00.000Z");

export const baseCompany = {
  id: "company-1",
  status: "ACTIVE",
};

export const baseAccount = {
  id: "account-1",
  code: "1000",
  name: "Cash",
  classification: "ASSET",
  status: "ACTIVE",
  successorAccountId: null,
};

export const baseAccount2 = {
  id: "account-2",
  code: "2000",
  name: "Accounts Payable",
  classification: "LIABILITY",
  status: "ACTIVE",
  successorAccountId: null,
};

export const receivableAccount = {
  id: "account-receivable",
  code: "1100",
  name: "Accounts Receivable",
  classification: "RECEIVABLE",
  status: "ACTIVE",
  successorAccountId: null,
};

export const payableAccount = {
  id: "account-payable",
  code: "2100",
  name: "Accounts Payable Control",
  classification: "PAYABLE",
  status: "ACTIVE",
  successorAccountId: null,
};

export const inactiveAccount = {
  ...baseAccount,
  id: "account-inactive",
  status: "INACTIVE",
  successorAccountId: null,
};

export const deprecatedAccount = {
  ...baseAccount,
  id: "account-deprecated",
  status: "DEPRECATED",
  successorAccountId: baseAccount.id,
};

export const retainedEarningsAccount = {
  id: "account-retained-earnings",
  code: "3100",
  name: "Retained Earnings",
  classification: "RETAINED_EARNINGS",
  status: "ACTIVE",
  successorAccountId: null,
};

export const revenueAccount = {
  id: "account-revenue",
  code: "4000",
  name: "Sales Revenue",
  classification: "REVENUE",
  status: "ACTIVE",
  successorAccountId: null,
};

export const expenseAccount = {
  id: "account-expense",
  code: "5000",
  name: "Cost of Goods Sold",
  classification: "EXPENSE",
  status: "ACTIVE",
  successorAccountId: null,
};

export const baseAccountGroup = {
  id: "account-group-1",
  name: "Current Assets",
  code: "CA",
};

export const baseChartOfAccounts = {
  id: "coa-1",
  companyId: baseCompany.id,
  status: "ACTIVE",
};

export const baseCurrency = {
  id: "currency-1",
  code: "USD",
  isActive: true,
};

export const baseUser = {
  id: "user-1",
  status: "ACTIVE",
};

export const baseFiscalYear = {
  id: "fiscal-year-1",
  companyId: baseCompany.id,
  name: "FY 2024",
  startDate: new Date("2024-01-01"),
  endDate: new Date("2024-12-31"),
  yearEndCloseExecuted: false,
  createdAt,
  updatedAt: null,
} as const satisfies FiscalYear<Schema>;

export const baseFiscalYear2 = {
  ...baseFiscalYear,
  id: "fiscal-year-2",
  name: "FY 2025",
  startDate: new Date("2025-01-01"),
  endDate: new Date("2025-12-31"),
} as const satisfies FiscalYear<Schema>;

export const baseAccountingPeriod = {
  id: "period-1",
  companyId: baseCompany.id,
  fiscalYearId: baseFiscalYear.id,
  name: "January 2024",
  startDate: new Date("2024-01-01"),
  endDate: new Date("2024-01-31"),
  periodType: "OPERATING",
  status: "OPEN",
  createdAt,
  updatedAt: null,
} as const satisfies AccountingPeriod<Schema>;

export const neverOpenedPeriod = {
  ...baseAccountingPeriod,
  id: "period-never-opened",
  status: "NEVER_OPENED",
} as const satisfies AccountingPeriod<Schema>;

export const futureEnterablePeriod = {
  ...baseAccountingPeriod,
  id: "period-future-enterable",
  status: "FUTURE_ENTERABLE",
} as const satisfies AccountingPeriod<Schema>;

export const closedPeriod = {
  ...baseAccountingPeriod,
  id: "period-closed",
  status: "CLOSED",
} as const satisfies AccountingPeriod<Schema>;

export const permanentlyClosedPeriod = {
  ...baseAccountingPeriod,
  id: "period-permanently-closed",
  status: "PERMANENTLY_CLOSED",
} as const satisfies AccountingPeriod<Schema>;

export const lastPeriodOfYear = {
  ...baseAccountingPeriod,
  id: "period-last",
  name: "December 2024",
  startDate: new Date("2024-12-01"),
  endDate: new Date("2024-12-31"),
} as const satisfies AccountingPeriod<Schema>;

export const baseJournalEntry = {
  id: "journal-entry-1",
  companyId: baseCompany.id,
  accountingPeriodId: baseAccountingPeriod.id,
  entryDate: new Date("2024-01-15"),
  journalType: "MISCELLANEOUS",
  referenceNumber: "JE-001",
  status: "DRAFT",
  description: "Test journal entry",
  sourceDocumentReference: null,
  adjustingEntryType: null,
  reversalOfId: null,
  postedAt: null,
  createdAt,
  updatedAt: null,
} as const satisfies JournalEntry<Schema>;

export const postedJournalEntry = {
  ...baseJournalEntry,
  id: "journal-entry-2",
  referenceNumber: "JE-002",
  status: "POSTED",
  postedAt: createdAt,
} as const satisfies JournalEntry<Schema>;

export const reversedJournalEntry = {
  ...baseJournalEntry,
  id: "journal-entry-3",
  referenceNumber: "JE-003",
  status: "REVERSED",
  postedAt: createdAt,
} as const satisfies JournalEntry<Schema>;

export const baseJournalLine = {
  id: "journal-line-1",
  journalEntryId: baseJournalEntry.id,
  accountId: baseAccount.id,
  debitAmount: 1000,
  creditAmount: null,
  description: "Debit line",
  currencyCode: null,
  exchangeRate: null,
  functionalDebitAmount: 1000,
  functionalCreditAmount: null,
  createdAt,
  updatedAt: null,
} as const satisfies JournalLine<Schema>;

export const baseJournalLine2 = {
  id: "journal-line-2",
  journalEntryId: baseJournalEntry.id,
  accountId: baseAccount2.id,
  debitAmount: null,
  creditAmount: 1000,
  description: "Credit line",
  currencyCode: null,
  exchangeRate: null,
  functionalDebitAmount: null,
  functionalCreditAmount: 1000,
  createdAt,
  updatedAt: null,
} as const satisfies JournalLine<Schema>;

export const basePeriodClose = {
  id: "period-close-1",
  accountingPeriodId: baseAccountingPeriod.id,
  status: "NOT_STARTED",
  initiatedByUserId: baseUser.id,
  completionPercentage: 0,
  subledgerVerificationDetails: null,
  trialBalanceId: null,
  preliminaryStatementIds: null,
  approvedByUserId: null,
  finalizedByUserId: null,
  yearEndCloseExecuted: false,
  createdAt,
  updatedAt: null,
} as const satisfies PeriodClose<Schema>;

export const inProgressPeriodClose = {
  ...basePeriodClose,
  id: "period-close-in-progress",
  status: "IN_PROGRESS",
  completionPercentage: 12,
} as const satisfies PeriodClose<Schema>;

export const subledgersVerifiedPeriodClose = {
  ...basePeriodClose,
  id: "period-close-subledgers-verified",
  status: "SUBLEDGERS_VERIFIED",
  completionPercentage: 25,
} as const satisfies PeriodClose<Schema>;

export const adjustmentsPostedPeriodClose = {
  ...basePeriodClose,
  id: "period-close-adjustments-posted",
  status: "ADJUSTMENTS_POSTED",
  completionPercentage: 50,
} as const satisfies PeriodClose<Schema>;

export const trialBalanceVerifiedPeriodClose = {
  ...basePeriodClose,
  id: "period-close-tb-verified",
  status: "TRIAL_BALANCE_VERIFIED",
  completionPercentage: 62,
} as const satisfies PeriodClose<Schema>;

export const underReviewPeriodClose = {
  ...basePeriodClose,
  id: "period-close-under-review",
  status: "UNDER_REVIEW",
  completionPercentage: 75,
} as const satisfies PeriodClose<Schema>;

export const softLockedPeriodClose = {
  ...basePeriodClose,
  id: "period-close-soft-locked",
  status: "SOFT_LOCKED",
  completionPercentage: 87,
  approvedByUserId: baseUser.id,
} as const satisfies PeriodClose<Schema>;

export const permanentlyClosedPeriodClose = {
  ...basePeriodClose,
  id: "period-close-permanently-closed",
  status: "PERMANENTLY_CLOSED",
  completionPercentage: 100,
} as const satisfies PeriodClose<Schema>;

export const baseTrialBalance = {
  id: "trial-balance-1",
  companyId: baseCompany.id,
  accountingPeriodId: baseAccountingPeriod.id,
  chartOfAccountsId: baseChartOfAccounts.id,
  variant: "PRE_CLOSE",
  generatedAt: createdAt,
  totalDebits: 10000,
  totalCredits: 10000,
  isBalanced: true,
  createdAt,
  updatedAt: null,
} as const satisfies TrialBalance<Schema>;

export const baseTrialBalanceLine = {
  id: "trial-balance-line-1",
  trialBalanceId: baseTrialBalance.id,
  accountId: baseAccount.id,
  accountGroupId: baseAccountGroup.id,
  accountCode: "1000",
  accountName: "Cash",
  openingBalance: 0,
  periodDebits: 5000,
  periodCredits: 2000,
  closingBalance: 3000,
  sortOrder: 1,
  createdAt,
  updatedAt: null,
} as const satisfies TrialBalanceLine<Schema>;

export const baseFinancialStatement = {
  id: "financial-statement-1",
  companyId: baseCompany.id,
  chartOfAccountsId: baseChartOfAccounts.id,
  trialBalanceId: baseTrialBalance.id,
  comparativePeriodTrialBalanceId: null,
  statementType: "BALANCE_SHEET",
  status: "DRAFT",
  reportingDate: new Date("2024-01-31"),
  startDate: null,
  endDate: null,
  cashFlowMethod: null,
  generatedAt: createdAt,
  finalizedAt: null,
  createdAt,
  updatedAt: null,
} as const satisfies FinancialStatement<Schema>;

export const finalizedFinancialStatement = {
  ...baseFinancialStatement,
  id: "financial-statement-2",
  status: "FINALIZED",
  finalizedAt: createdAt,
} as const satisfies FinancialStatement<Schema>;

export const baseFinancialStatementLineItem = {
  id: "fs-line-item-1",
  financialStatementId: baseFinancialStatement.id,
  accountId: baseAccount.id,
  accountGroupId: baseAccountGroup.id,
  label: "Cash",
  amount: 3000,
  comparativeAmount: null,
  lineType: "DETAIL",
  sectionType: "Current Assets",
  sortOrder: 1,
  createdAt,
  updatedAt: null,
} as const satisfies FinancialStatementLineItem<Schema>;
