/**
 * ContractAddendum Entity
 * Addendas y modificaciones a contratos
 *
 * @module Contracts
 * @table contracts.contract_addendums
 */

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';
import { Contract } from './contract.entity';

export type AddendumType = 'extension' | 'amount_increase' | 'amount_decrease' | 'scope_change' | 'termination' | 'other';
export type AddendumStatus = 'draft' | 'review' | 'approved' | 'rejected';

@Entity({ schema: 'contracts', name: 'contract_addendums' })
@Index(['tenantId', 'addendumNumber'], { unique: true })
export class ContractAddendum {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'contract_id', type: 'uuid' })
  contractId: string;

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

  @Column({ name: 'addendum_type', type: 'varchar', length: 30 })
  addendumType: AddendumType;

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

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

  @Column({ name: 'effective_date', type: 'date' })
  effectiveDate: Date;

  // Changes
  @Column({ name: 'new_end_date', type: 'date', nullable: true })
  newEndDate: Date;

  @Column({ name: 'amount_change', type: 'decimal', precision: 16, scale: 2, default: 0 })
  amountChange: number;

  @Column({ name: 'new_contract_amount', type: 'decimal', precision: 16, scale: 2, nullable: true })
  newContractAmount: number;

  @Column({ name: 'scope_changes', type: 'text', nullable: true })
  scopeChanges: string;

  // Status
  @Column({ type: 'varchar', length: 20, default: 'draft' })
  status: AddendumStatus;

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

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

  @Column({ name: 'rejection_reason', type: 'text', nullable: true })
  rejectionReason: string;

  // Document
  @Column({ name: 'document_url', type: 'varchar', length: 500, nullable: true })
  documentUrl: 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;

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

  @ManyToOne(() => Contract, (c) => c.addendums, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'contract_id' })
  contract: Contract;

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

  @ManyToOne(() => User)
  @JoinColumn({ name: 'approved_by' })
  approvedBy: User;
}
