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

export type ScheduleType = 'regular' | 'holiday' | 'special';

@Entity({ name: 'branch_schedules', schema: 'core' })
export class BranchSchedule {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'branch_id', type: 'uuid' })
  branchId: string;

  // Identificacion
  @Column({ type: 'varchar', length: 100 })
  name: string;

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

  // Tipo
  @Column({ name: 'schedule_type', type: 'varchar', length: 30, default: 'regular' })
  scheduleType: ScheduleType;

  // Dia de la semana (0=domingo, 1=lunes, ..., 6=sabado) o fecha especifica
  @Index()
  @Column({ name: 'day_of_week', type: 'integer', nullable: true })
  dayOfWeek: number;

  @Index()
  @Column({ name: 'specific_date', type: 'date', nullable: true })
  specificDate: Date;

  // Horarios
  @Column({ name: 'open_time', type: 'time' })
  openTime: string;

  @Column({ name: 'close_time', type: 'time' })
  closeTime: string;

  // Turnos (si aplica)
  @Column({ type: 'jsonb', default: [] })
  shifts: Array<{
    name: string;
    start: string;
    end: string;
  }>;

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

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

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

  // Relaciones
  @ManyToOne(() => Branch, (branch) => branch.schedules, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'branch_id' })
  branch: Branch;
}
