import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  OneToMany,
} from 'typeorm';
import { Leave } from './leave.entity';
import { LeaveAllocation } from './leave-allocation.entity';

/**
 * Leave Type Category Enum
 */
export type LeaveTypeCategory =
  | 'vacation'
  | 'sick'
  | 'personal'
  | 'maternity'
  | 'paternity'
  | 'bereavement'
  | 'unpaid'
  | 'other';

/**
 * Allocation Type Enum
 */
export type AllocationType = 'fixed' | 'accrual' | 'unlimited';

/**
 * Leave Type Entity (schema: hr.leave_types)
 *
 * Configurable leave/absence types for the organization.
 * Defines rules for approval, allocation, and payment.
 *
 * Examples: Vacation, Sick Leave, Maternity, etc.
 *
 * RLS Policy: tenant_id = current_setting('app.current_tenant_id')
 */
@Entity({ name: 'leave_types', schema: 'hr' })
@Index(['tenantId', 'companyId', 'code'], { unique: true })
export class LeaveType {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

  @Column({ name: 'color', type: 'varchar', length: 7, default: '#3B82F6' })
  color: string;

  @Index()
  @Column({
    name: 'leave_category',
    type: 'enum',
    enum: ['vacation', 'sick', 'personal', 'maternity', 'paternity', 'bereavement', 'unpaid', 'other'],
    enumName: 'hr_leave_type_category',
    default: 'other',
  })
  leaveCategory: LeaveTypeCategory;

  @Column({
    name: 'allocation_type',
    type: 'enum',
    enum: ['fixed', 'accrual', 'unlimited'],
    enumName: 'hr_allocation_type',
    default: 'fixed',
  })
  allocationType: AllocationType;

  @Column({ name: 'requires_approval', type: 'boolean', default: true })
  requiresApproval: boolean;

  @Column({ name: 'requires_document', type: 'boolean', default: false })
  requiresDocument: boolean;

  // Limits
  @Column({ name: 'max_days_per_request', type: 'integer', nullable: true })
  maxDaysPerRequest: number | null;

  @Column({ name: 'max_days_per_year', type: 'integer', nullable: true })
  maxDaysPerYear: number | null;

  @Column({ name: 'min_days_notice', type: 'integer', default: 0 })
  minDaysNotice: number;

  // Payment
  @Column({ name: 'is_paid', type: 'boolean', default: true })
  isPaid: boolean;

  @Column({
    name: 'pay_percentage',
    type: 'decimal',
    precision: 5,
    scale: 2,
    default: 100,
  })
  payPercentage: number;

  @Index()
  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

  // Relations
  @OneToMany(() => Leave, (leave) => leave.leaveType)
  leaves: Leave[];

  @OneToMany(() => LeaveAllocation, (allocation) => allocation.leaveType)
  allocations: LeaveAllocation[];

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

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