import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Department } from './department.entity';

/**
 * Contract Type Enum
 */
export type ContractType = 'permanent' | 'temporary' | 'contractor' | 'internship' | 'part_time';

/**
 * Contract Status Enum
 */
export type ContractStatus = 'draft' | 'active' | 'expired' | 'terminated' | 'cancelled';

/**
 * Wage Type for payment frequency
 */
export type WageType = 'hourly' | 'daily' | 'weekly' | 'biweekly' | 'monthly' | 'annual';

/**
 * Contract Entity (schema: hr.contracts)
 *
 * Employment contracts with details about compensation, duration,
 * and terms of employment. Tracks contract lifecycle from draft to termination.
 *
 * RLS Policy: tenant_id = current_setting('app.current_tenant_id')
 */
@Entity({ name: 'contracts', schema: 'hr' })
export class Contract {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'company_id', type: 'uuid' })
  companyId: string;

  @Index()
  @Column({ name: 'employee_id', type: 'uuid' })
  employeeId: string;

  // Note: ManyToOne to Employee removed - construction Employee
  // has a different structure and does not have contracts back-reference.

  @Column({ name: 'name', type: 'varchar', length: 255 })
  name: string;

  @Column({ name: 'reference', type: 'varchar', length: 50, nullable: true })
  reference: string | null;

  @Index()
  @Column({
    name: 'contract_type',
    type: 'enum',
    enum: ['permanent', 'temporary', 'contractor', 'internship', 'part_time'],
    enumName: 'hr_contract_type',
    default: 'permanent',
  })
  contractType: ContractType;

  @Index()
  @Column({
    name: 'status',
    type: 'enum',
    enum: ['draft', 'active', 'expired', 'terminated', 'cancelled'],
    enumName: 'hr_contract_status',
    default: 'draft',
  })
  status: ContractStatus;

  @Index()
  @Column({ name: 'date_start', type: 'date' })
  dateStart: Date;

  @Column({ name: 'date_end', type: 'date', nullable: true })
  dateEnd: Date | null;

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

  // Note: ManyToOne to JobPosition removed - construction uses Puesto entity instead.

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

  @ManyToOne(() => Department, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'department_id' })
  department: Department | null;

  // Compensation
  @Column({ name: 'wage', type: 'decimal', precision: 15, scale: 2 })
  wage: number;

  @Column({
    name: 'wage_type',
    type: 'varchar',
    length: 20,
    default: 'monthly',
  })
  wageType: WageType;

  @Column({ name: 'currency', type: 'varchar', length: 3, default: 'MXN' })
  currency: string;

  // Schedule
  @Column({
    name: 'hours_per_week',
    type: 'decimal',
    precision: 5,
    scale: 2,
    nullable: true,
    default: 48,
  })
  hoursPerWeek: number | null;

  @Column({ name: 'schedule_type', type: 'varchar', length: 50, nullable: true })
  scheduleType: string | null;

  // Trial period
  @Column({ name: 'trial_period_months', type: 'integer', nullable: true, default: 0 })
  trialPeriodMonths: number | null;

  @Column({ name: 'trial_date_end', type: 'date', nullable: true })
  trialDateEnd: Date | null;

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

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

  // Audit
  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

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

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

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

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

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