import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
} from 'typeorm';

@Entity('monthly_financial_reports')
@Index('idx_monthly_financial_reports_tenant', ['tenantId'])
@Index('idx_monthly_financial_reports_month', ['reportMonth'])
@Index('idx_monthly_financial_reports_status', ['status'])
export class MonthlyFinancialReportORM {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  @Column({ name: 'report_month', type: 'varchar', length: 7 })
  reportMonth: string;

  // Revenue Statistics
  @Column({ name: 'total_revenue', type: 'decimal', precision: 12, scale: 2, default: 0 })
  totalRevenue: number;

  @Column({ name: 'assessment_revenue', type: 'decimal', precision: 12, scale: 2, default: 0 })
  assessmentRevenue: number;

  @Column({ name: 'consultation_revenue', type: 'decimal', precision: 12, scale: 2, default: 0 })
  consultationRevenue: number;

  @Column({ name: 'other_revenue', type: 'decimal', precision: 12, scale: 2, default: 0 })
  otherRevenue: number;

  // Refund Statistics
  @Column({ name: 'total_refunds', type: 'decimal', precision: 12, scale: 2, default: 0 })
  totalRefunds: number;

  @Column({ name: 'net_revenue', type: 'decimal', precision: 12, scale: 2, default: 0 })
  netRevenue: number;

  // Cost Statistics
  @Column({ name: 'api_cost', type: 'decimal', precision: 10, scale: 2, default: 0 })
  apiCost: number;

  @Column({ name: 'payment_fees', type: 'decimal', precision: 10, scale: 2, default: 0 })
  paymentFees: number;

  @Column({ name: 'other_costs', type: 'decimal', precision: 10, scale: 2, default: 0 })
  otherCosts: number;

  @Column({ name: 'total_costs', type: 'decimal', precision: 10, scale: 2, default: 0 })
  totalCosts: number;

  // Profit Statistics
  @Column({ name: 'gross_profit', type: 'decimal', precision: 12, scale: 2, default: 0 })
  grossProfit: number;

  @Column({ name: 'gross_margin', type: 'decimal', precision: 5, scale: 4, default: 0 })
  grossMargin: number;

  // Order Statistics
  @Column({ name: 'total_orders', type: 'int', default: 0 })
  totalOrders: number;

  @Column({ name: 'successful_orders', type: 'int', default: 0 })
  successfulOrders: number;

  @Column({ name: 'avg_order_amount', type: 'decimal', precision: 10, scale: 2, default: 0 })
  avgOrderAmount: number;

  // Revenue Breakdown (JSONB)
  @Column({ name: 'revenue_by_category', type: 'jsonb', default: '{}' })
  revenueByCategory: Record<string, number>;

  @Column({ name: 'revenue_by_channel', type: 'jsonb', default: '{}' })
  revenueByChannel: Record<string, number>;

  // Report Management
  @Column({ type: 'varchar', length: 20, default: 'DRAFT' })
  status: string;

  @Column({ name: 'confirmed_by', type: 'uuid', nullable: true })
  confirmedBy: string | null;

  @Column({ name: 'confirmed_at', type: 'timestamptz', nullable: true })
  confirmedAt: Date | null;

  @Column({ type: 'text', nullable: true })
  notes: string | null;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;
}
