import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  OneToMany,
} from 'typeorm';
import { TaskEntity } from './task.entity';
import { MilestoneEntity } from './milestone.entity';
import { ProjectMemberEntity } from './project-member.entity';
import { TimesheetEntity } from './timesheet.entity';

export enum ProjectStatus {
  DRAFT = 'draft',
  ACTIVE = 'active',
  COMPLETED = 'completed',
  CANCELLED = 'cancelled',
  ON_HOLD = 'on_hold',
}

export enum ProjectPrivacy {
  PUBLIC = 'public',
  PRIVATE = 'private',
  FOLLOWERS = 'followers',
}

@Entity({ schema: 'projects', name: 'projects' })
@Index('idx_projects_tenant', ['tenantId'])
@Index('idx_projects_company', ['companyId'])
@Index('idx_projects_manager', ['managerId'])
@Index('idx_projects_partner', ['partnerId'])
@Index('idx_projects_status', ['status'])
@Index('idx_projects_code', ['code'])
export class ProjectEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

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

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

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

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

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

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

  @Column({
    type: 'enum',
    enum: ProjectPrivacy,
    default: ProjectPrivacy.PUBLIC,
    nullable: false,
  })
  privacy: ProjectPrivacy;

  @Column({ type: 'boolean', default: true, nullable: false, name: 'allow_timesheets' })
  allowTimesheets: boolean;

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

  // Relations
  @OneToMany(() => TaskEntity, (task) => task.project)
  tasks: TaskEntity[];

  @OneToMany(() => MilestoneEntity, (milestone) => milestone.project)
  milestones: MilestoneEntity[];

  @OneToMany(() => ProjectMemberEntity, (member) => member.project)
  members: ProjectMemberEntity[];

  @OneToMany(() => TimesheetEntity, (timesheet) => timesheet.project)
  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;
}
