import type {
  CostElement,
  CostCenter,
  CostCenterHierarchy,
  CostCenterHierarchyNode,
  ProfitCenter,
  ProfitCenterHierarchy,
  ProfitCenterHierarchyNode,
  ProfitCenterHierarchyAssignment,
  InternalOrder,
  AllocationCycle,
  AllocationRule,
  AllocationResult,
  PlanVersion,
  Budget,
  BudgetLineItem,
  Commitment,
  VarianceReport,
  VarianceReportLine,
  Schema,
} from "../lib/types";

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

// Cross-module fixtures (minimal shape for validation)
export const baseCompany = {
  id: "company-1",
  status: "ACTIVE",
};

export const baseAccount = {
  id: "account-1",
  code: "5000",
  name: "Material Costs",
  classification: "EXPENSE",
  status: "ACTIVE",
  chartOfAccountsId: "coa-1",
};

export const baseAccount2 = {
  id: "account-2",
  code: "5100",
  name: "Labor Costs",
  classification: "EXPENSE",
  status: "ACTIVE",
  chartOfAccountsId: "coa-1",
};

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

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

export const inactiveChartOfAccounts = {
  ...baseChartOfAccounts,
  id: "coa-inactive",
  status: "INACTIVE",
};

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

export const baseUser2 = {
  id: "user-2",
  status: "ACTIVE",
};

export const baseDepartment = {
  id: "department-1",
  name: "IT Department",
};

export const baseAccountingPeriod = {
  id: "period-1",
  companyId: baseCompany.id,
  fiscalYearId: "fiscal-year-1",
  status: "OPEN",
};

export const closedAccountingPeriod = {
  ...baseAccountingPeriod,
  id: "period-closed",
  status: "CLOSED",
};

export const baseFiscalYear = {
  id: "fiscal-year-1",
  companyId: baseCompany.id,
};

// CostElement fixtures
export const baseCostElement = {
  id: "cost-element-1",
  code: "CE-001",
  name: "Material Overhead",
  type: "PRIMARY",
  category: "overhead",
  description: "Material overhead costs",
  glAccountId: baseAccount.id,
  companyId: baseCompany.id,
  validFrom: null,
  validTo: null,
  status: "DRAFT",
  createdAt,
  updatedAt: null,
} as const satisfies CostElement<Schema>;

export const activeCostElement = {
  ...baseCostElement,
  id: "cost-element-active",
  status: "ACTIVE",
} as const satisfies CostElement<Schema>;

export const inactiveCostElement = {
  ...baseCostElement,
  id: "cost-element-inactive",
  status: "INACTIVE",
} as const satisfies CostElement<Schema>;

export const secondaryCostElement = {
  ...baseCostElement,
  id: "cost-element-secondary",
  code: "CE-SEC-001",
  name: "Assessment Overhead",
  type: "SECONDARY",
  category: "assessment",
  glAccountId: null,
  status: "ACTIVE",
} as const satisfies CostElement<Schema>;

// CostCenterHierarchy fixtures
export const baseCostCenterHierarchy = {
  id: "cc-hierarchy-1",
  name: "Standard Hierarchy",
  version: "V1",
  companyId: baseCompany.id,
  createdAt,
  updatedAt: null,
} as const satisfies CostCenterHierarchy<Schema>;

// CostCenterHierarchyNode fixtures
export const rootCostCenterNode = {
  id: "cc-node-root",
  name: "Root",
  hierarchyId: baseCostCenterHierarchy.id,
  parentNodeId: null,
  sortOrder: 1,
  level: 0,
  createdAt,
  updatedAt: null,
} as const satisfies CostCenterHierarchyNode<Schema>;

export const childCostCenterNode = {
  id: "cc-node-child-1",
  name: "Administration",
  hierarchyId: baseCostCenterHierarchy.id,
  parentNodeId: rootCostCenterNode.id,
  sortOrder: 1,
  level: 1,
  createdAt,
  updatedAt: null,
} as const satisfies CostCenterHierarchyNode<Schema>;

export const childCostCenterNode2 = {
  id: "cc-node-child-2",
  name: "Production",
  hierarchyId: baseCostCenterHierarchy.id,
  parentNodeId: rootCostCenterNode.id,
  sortOrder: 2,
  level: 1,
  createdAt,
  updatedAt: null,
} as const satisfies CostCenterHierarchyNode<Schema>;

// CostCenter fixtures
export const baseCostCenter = {
  id: "cost-center-1",
  code: "CC-001",
  name: "IT Department",
  description: "Information Technology",
  category: "overhead",
  responsiblePersonId: baseUser.id,
  departmentId: baseDepartment.id,
  companyId: baseCompany.id,
  hierarchyNodeId: childCostCenterNode.id,
  validFrom: new Date("2024-01-01"),
  validTo: null,
  createdAt,
  updatedAt: null,
} as const satisfies CostCenter<Schema>;

export const baseCostCenter2 = {
  ...baseCostCenter,
  id: "cost-center-2",
  code: "CC-002",
  name: "Finance Department",
  hierarchyNodeId: childCostCenterNode2.id,
} as const satisfies CostCenter<Schema>;

// ProfitCenterHierarchy fixtures
export const baseProfitCenterHierarchy = {
  id: "pc-hierarchy-1",
  name: "Product Lines",
  companyId: baseCompany.id,
  createdAt,
  updatedAt: null,
} as const satisfies ProfitCenterHierarchy<Schema>;

// ProfitCenterHierarchyNode fixtures
export const rootProfitCenterNode = {
  id: "pc-node-root",
  name: "Root",
  hierarchyId: baseProfitCenterHierarchy.id,
  parentNodeId: null,
  sortOrder: 1,
  level: 0,
  createdAt,
  updatedAt: null,
} as const satisfies ProfitCenterHierarchyNode<Schema>;

export const childProfitCenterNode = {
  id: "pc-node-child-1",
  name: "Product Line A",
  hierarchyId: baseProfitCenterHierarchy.id,
  parentNodeId: rootProfitCenterNode.id,
  sortOrder: 1,
  level: 1,
  createdAt,
  updatedAt: null,
} as const satisfies ProfitCenterHierarchyNode<Schema>;

// ProfitCenter fixtures
export const baseProfitCenter = {
  id: "profit-center-1",
  code: "PC-001",
  name: "Product Line Alpha",
  description: "Alpha product line",
  responsiblePersonId: baseUser.id,
  companyId: baseCompany.id,
  validFrom: new Date("2024-01-01"),
  validTo: null,
  status: "DRAFT",
  createdAt,
  updatedAt: null,
} as const satisfies ProfitCenter<Schema>;

export const activeProfitCenter = {
  ...baseProfitCenter,
  id: "profit-center-active",
  status: "ACTIVE",
} as const satisfies ProfitCenter<Schema>;

export const inactiveProfitCenter = {
  ...baseProfitCenter,
  id: "profit-center-inactive",
  status: "INACTIVE",
} as const satisfies ProfitCenter<Schema>;

// InternalOrder fixtures
export const baseInternalOrder = {
  id: "internal-order-1",
  orderNumber: "IO-001",
  orderType: "OVERHEAD",
  description: "IT Infrastructure Upgrade",
  responsiblePersonId: baseUser.id,
  costCenterId: baseCostCenter.id,
  companyId: baseCompany.id,
  validFrom: null,
  validTo: null,
  status: "CREATED",
  createdAt,
  updatedAt: null,
} as const satisfies InternalOrder<Schema>;

export const releasedInternalOrder = {
  ...baseInternalOrder,
  id: "internal-order-released",
  status: "RELEASED",
} as const satisfies InternalOrder<Schema>;

export const techCompletedInternalOrder = {
  ...baseInternalOrder,
  id: "internal-order-tech-completed",
  status: "TECHNICALLY_COMPLETED",
} as const satisfies InternalOrder<Schema>;

export const closedInternalOrder = {
  ...baseInternalOrder,
  id: "internal-order-closed",
  status: "CLOSED",
} as const satisfies InternalOrder<Schema>;

// AllocationCycle fixtures
export const baseAllocationCycle = {
  id: "allocation-cycle-1",
  name: "Q1 2024 Overhead",
  allocationType: "ASSESSMENT",
  companyId: baseCompany.id,
  accountingPeriodId: baseAccountingPeriod.id,
  status: "DRAFT",
  createdAt,
  updatedAt: null,
} as const satisfies AllocationCycle<Schema>;

export const validatedAllocationCycle = {
  ...baseAllocationCycle,
  id: "allocation-cycle-validated",
  status: "VALIDATED",
} as const satisfies AllocationCycle<Schema>;

export const executedAllocationCycle = {
  ...baseAllocationCycle,
  id: "allocation-cycle-executed",
  status: "EXECUTED",
} as const satisfies AllocationCycle<Schema>;

export const postedAllocationCycle = {
  ...baseAllocationCycle,
  id: "allocation-cycle-posted",
  status: "POSTED",
} as const satisfies AllocationCycle<Schema>;

export const reversedAllocationCycle = {
  ...baseAllocationCycle,
  id: "allocation-cycle-reversed",
  status: "REVERSED",
} as const satisfies AllocationCycle<Schema>;

// AllocationRule fixtures
export const baseAllocationRule = {
  id: "allocation-rule-1",
  allocationCycleId: baseAllocationCycle.id,
  senderCostCenterId: baseCostCenter.id,
  receiverType: "CostCenter",
  receiverId: baseCostCenter2.id,
  percentage: 100,
  createdAt,
  updatedAt: null,
} as const satisfies AllocationRule<Schema>;

// AllocationResult fixtures
export const baseAllocationResult = {
  id: "allocation-result-1",
  allocationCycleId: baseAllocationCycle.id,
  senderCostCenterId: baseCostCenter.id,
  receiverType: "CostCenter",
  receiverId: baseCostCenter2.id,
  allocatedAmount: 5000,
  costElementId: activeCostElement.id,
  createdAt,
  updatedAt: null,
} as const satisfies AllocationResult<Schema>;

// PlanVersion fixtures
export const basePlanVersion = {
  id: "plan-version-1",
  name: "FY2024 Budget",
  type: "BUDGET",
  baseVersionId: null,
  companyId: baseCompany.id,
  createdAt,
  updatedAt: null,
} as const satisfies PlanVersion<Schema>;

// Budget fixtures
export const baseBudget = {
  id: "budget-1",
  name: "IT Budget 2024",
  description: "Information Technology budget for FY2024",
  budgetHolderId: baseUser.id,
  fiscalYearId: baseFiscalYear.id,
  companyId: baseCompany.id,
  costObjectType: "CostCenter",
  costObjectId: baseCostCenter.id,
  planVersionId: basePlanVersion.id,
  totalAmount: 100000,
  status: "DRAFT",
  rejectionReason: null,
  createdByUserId: baseUser.id,
  createdAt,
  updatedAt: null,
} as const satisfies Budget<Schema>;

export const submittedBudget = {
  ...baseBudget,
  id: "budget-submitted",
  status: "SUBMITTED",
} as const satisfies Budget<Schema>;

export const approvedBudget = {
  ...baseBudget,
  id: "budget-approved",
  status: "APPROVED",
} as const satisfies Budget<Schema>;

export const activeBudget = {
  ...baseBudget,
  id: "budget-active",
  status: "ACTIVE",
} as const satisfies Budget<Schema>;

export const closedBudget = {
  ...baseBudget,
  id: "budget-closed",
  status: "CLOSED",
} as const satisfies Budget<Schema>;

// BudgetLineItem fixtures
export const baseBudgetLineItem = {
  id: "budget-line-item-1",
  budgetId: baseBudget.id,
  costElementId: activeCostElement.id,
  glAccountId: null,
  accountingPeriodId: baseAccountingPeriod.id,
  plannedAmount: 10000,
  createdAt,
  updatedAt: null,
} as const satisfies BudgetLineItem<Schema>;

// Commitment fixtures
export const baseCommitment = {
  id: "commitment-1",
  costElementId: activeCostElement.id,
  costObjectType: "CostCenter",
  costObjectId: baseCostCenter.id,
  accountingPeriodId: baseAccountingPeriod.id,
  amount: 5000,
  consumedAmount: 0,
  purchaseOrderId: "purchase-order-1",
  companyId: baseCompany.id,
  status: "OPEN",
  createdAt,
  updatedAt: null,
} as const satisfies Commitment<Schema>;

export const partiallyConsumedCommitment = {
  ...baseCommitment,
  id: "commitment-partially-consumed",
  consumedAmount: 2000,
} as const satisfies Commitment<Schema>;

export const fullyConsumedCommitment = {
  ...baseCommitment,
  id: "commitment-fully-consumed",
  consumedAmount: 5000,
  status: "CONSUMED",
} as const satisfies Commitment<Schema>;

// VarianceReport fixtures
export const baseVarianceReport = {
  id: "variance-report-1",
  costObjectType: "CostCenter",
  costObjectId: baseCostCenter.id,
  accountingPeriodId: baseAccountingPeriod.id,
  companyId: baseCompany.id,
  totalPlanned: 10000,
  totalActual: 12000,
  totalVariance: 2000,
  status: "DRAFT",
  createdAt,
  updatedAt: null,
} as const satisfies VarianceReport<Schema>;

export const finalVarianceReport = {
  ...baseVarianceReport,
  id: "variance-report-final",
  status: "FINAL",
} as const satisfies VarianceReport<Schema>;

// ProfitCenterHierarchyAssignment fixtures
export const baseProfitCenterHierarchyAssignment = {
  id: "pc-hierarchy-assignment-1",
  profitCenterId: "profit-center-1",
  hierarchyId: baseProfitCenterHierarchy.id,
  hierarchyNodeId: childProfitCenterNode.id,
  createdAt,
  updatedAt: null,
} as const satisfies ProfitCenterHierarchyAssignment<Schema>;

// VarianceReportLine fixtures
export const baseVarianceReportLine = {
  id: "variance-report-line-1",
  varianceReportId: baseVarianceReport.id,
  costElementId: activeCostElement.id,
  accountingPeriodId: baseAccountingPeriod.id,
  plannedAmount: 10000,
  actualAmount: 12000,
  varianceAmount: 2000,
  variancePercentage: 20,
  varianceDirection: "UNFAVORABLE",
  createdAt,
  updatedAt: null,
} as const satisfies VarianceReportLine<Schema>;
