/**
 * Subcontractor Entity
 * Catálogo de subcontratistas
 *
 * @module Contracts
 * @table contracts.subcontractors
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';

export type SubcontractorSpecialty = 'cimentacion' | 'estructura' | 'instalaciones_electricas' | 'instalaciones_hidraulicas' | 'acabados' | 'urbanizacion' | 'carpinteria' | 'herreria' | 'otros';
export type SubcontractorStatus = 'active' | 'inactive' | 'blacklisted';

@Entity({ schema: 'construction', name: 'subcontratistas' })
@Index(['tenantId', 'code'], { unique: true })
@Index(['tenantId', 'rfc'], { unique: true })
export class Subcontractor {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  @Column({ type: 'varchar', length: 13 })
  rfc: string;

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

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

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

  @Column({ name: 'contact_name', type: 'varchar', length: 200, nullable: true })
  contactName: string;

  @Column({ name: 'contact_phone', type: 'varchar', length: 20, nullable: true })
  contactPhone: string;

  @Column({ name: 'primary_specialty', type: 'varchar', length: 50 })
  primarySpecialty: SubcontractorSpecialty;

  @Column({ name: 'secondary_specialties', type: 'simple-array', nullable: true })
  secondarySpecialties: string[];

  @Column({ type: 'varchar', length: 20, default: 'active' })
  status: SubcontractorStatus;

  // Performance tracking
  @Column({ name: 'total_contracts', type: 'integer', default: 0 })
  totalContracts: number;

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

  @Column({ name: 'average_rating', type: 'decimal', precision: 3, scale: 2, default: 0 })
  averageRating: number;

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

  // Financial info
  @Column({ name: 'bank_name', type: 'varchar', length: 100, nullable: true })
  bankName: string;

  @Column({ name: 'bank_account', type: 'varchar', length: 30, nullable: true })
  bankAccount: string;

  @Column({ type: 'varchar', length: 18, nullable: true })
  clabe: string;

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

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

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

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

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

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

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

  // Relations
  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'created_by' })
  createdBy: User;
}
