import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { LeaveType } from './leave-type.entity';
import { LeaveAllocation } from './leave-allocation.entity';

/**
 * Leave Status Enum
 */
export type LeaveStatus = 'draft' | 'submitted' | 'approved' | 'rejected' | 'cancelled';

/**
 * Half Day Type
 */
export type HalfDayType = 'morning' | 'afternoon';

/**
 * Leave Entity (schema: hr.leaves)
 *
 * Leave/absence requests from employees.
 * Tracks the full lifecycle from draft to approval/rejection.
 *
 * RLS Policy: tenant_id = current_setting('app.current_tenant_id')
 */
@Entity({ name: 'leaves', schema: 'hr' })
export class Leave {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Index()
  @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 leaves back-reference.

  @Index()
  @Column({ name: 'leave_type_id', type: 'uuid' })
  leaveTypeId: string;

  @ManyToOne(() => LeaveType, (leaveType) => leaveType.leaves, {
    onDelete: 'RESTRICT',
  })
  @JoinColumn({ name: 'leave_type_id' })
  leaveType: LeaveType;

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

  @ManyToOne(() => LeaveAllocation, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'allocation_id' })
  allocation: LeaveAllocation | null;

  // Period
  @Index()
  @Column({ name: 'date_from', type: 'date' })
  dateFrom: Date;

  @Column({ name: 'date_to', type: 'date' })
  dateTo: Date;

  @Column({ name: 'days_requested', type: 'decimal', precision: 5, scale: 2 })
  daysRequested: number;

  // Half day support
  @Column({ name: 'is_half_day', type: 'boolean', default: false })
  isHalfDay: boolean;

  @Column({
    name: 'half_day_type',
    type: 'varchar',
    length: 20,
    nullable: true,
  })
  halfDayType: HalfDayType | null;

  @Index()
  @Column({
    name: 'status',
    type: 'enum',
    enum: ['draft', 'submitted', 'approved', 'rejected', 'cancelled'],
    enumName: 'hr_leave_status',
    default: 'draft',
  })
  status: LeaveStatus;

  // Approval
  @Index()
  @Column({ name: 'approver_id', type: 'uuid', nullable: true })
  approverId: string | null;

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

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

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

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

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

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

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

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

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

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