import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import { ProjectEntity } from './project.entity';
import { TimesheetEntity } from './timesheet.entity';

export enum TaskStatus {
  TODO = 'todo',
  IN_PROGRESS = 'in_progress',
  REVIEW = 'review',
  DONE = 'done',
  CANCELLED = 'cancelled',
}

export enum TaskPriority {
  LOW = 'low',
  NORMAL = 'normal',
  HIGH = 'high',
  URGENT = 'urgent',
}

@Entity({ schema: 'projects', name: 'tasks' })
@Index('idx_tasks_tenant', ['tenantId'])
@Index('idx_tasks_project', ['projectId'])
@Index('idx_tasks_stage', ['stageId'])
@Index('idx_tasks_parent', ['parentId'])
@Index('idx_tasks_assigned', ['assignedTo'])
@Index('idx_tasks_status', ['status'])
@Index('idx_tasks_priority', ['priority'])
@Index('idx_tasks_deadline', ['dateDeadline'])
export class TaskEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ type: 'uuid', nullable: false, name: 'project_id' })
  projectId: string;

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

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

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

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

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

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

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

  @Column({
    type: 'enum',
    enum: TaskPriority,
    default: TaskPriority.NORMAL,
    nullable: false,
  })
  priority: TaskPriority;

  @Column({
    type: 'enum',
    enum: TaskStatus,
    default: TaskStatus.TODO,
    nullable: false,
  })
  status: TaskStatus;

  @Column({ type: 'int', default: 0, nullable: false })
  sequence: number;

  @Column({ type: 'varchar', length: 20, nullable: true })
  color: string | null;

  // Relations
  @ManyToOne(() => ProjectEntity, (project) => project.tasks, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'project_id' })
  project: ProjectEntity;

  @ManyToOne(() => TaskEntity, (task) => task.children, { onDelete: 'SET NULL' })
  @JoinColumn({ name: 'parent_id' })
  parent: TaskEntity | null;

  @OneToMany(() => TaskEntity, (task) => task.parent)
  children: TaskEntity[];

  @OneToMany(() => TimesheetEntity, (timesheet) => timesheet.task)
  timesheets: TimesheetEntity[];

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

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

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp', nullable: true })
  updatedAt: Date | null;

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

  @Column({ type: 'timestamp', nullable: true, name: 'deleted_at' })
  deletedAt: Date | null;

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