/**
 * DepreciationSchedule Entity - Programacion de Depreciacion
 *
 * Configuracion de depreciacion para cada activo fijo.
 *
 * @module Assets (MAE-015)
 * @table assets.depreciation_schedule
 * @gap GAP-003
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Asset } from './asset.entity';
import { DepreciationEntry } from './depreciation-entry.entity';

export type DepreciationMethod =
  | 'straight_line'
  | 'declining_balance'
  | 'double_declining'
  | 'sum_of_years'
  | 'units_of_production';

export type AssetDepreciationType = 'equipment' | 'machinery' | 'vehicle' | 'tool';

@Entity({ schema: 'assets', name: 'depreciation_schedule' })
@Index(['tenantId', 'assetId'], { unique: true })
@Index(['tenantId'])
@Index(['tenantId', 'isActive'])
@Index(['tenantId', 'isFullyDepreciated'])
export class DepreciationSchedule {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Activo
  @Column({ name: 'asset_id', type: 'uuid' })
  assetId!: string;

  @ManyToOne(() => Asset)
  @JoinColumn({ name: 'asset_id' })
  asset?: Asset;

  // Tipo de activo
  @Column({ name: 'asset_type', length: 20 })
  assetType!: AssetDepreciationType;

  // Metodo de depreciacion
  @Column({
    type: 'enum',
    enum: ['straight_line', 'declining_balance', 'double_declining', 'sum_of_years', 'units_of_production'],
    enumName: 'depreciation_method',
    default: 'straight_line',
  })
  method!: DepreciationMethod;

  // Valores
  @Column({ name: 'original_value', type: 'decimal', precision: 18, scale: 2 })
  originalValue!: number;

  @Column({ name: 'salvage_value', type: 'decimal', precision: 18, scale: 2, default: 0 })
  salvageValue!: number;

  // Campos calculados (GENERATED ALWAYS AS en DDL)
  @Column({ name: 'depreciable_amount', type: 'decimal', precision: 18, scale: 2, insert: false, update: false })
  depreciableAmount?: number;

  // Vida util
  @Column({ name: 'useful_life_months', type: 'int' })
  usefulLifeMonths!: number;

  @Column({ name: 'useful_life_units', type: 'int', nullable: true })
  usefulLifeUnits?: number;

  // Depreciacion mensual calculada (GENERATED en DDL)
  @Column({ name: 'monthly_depreciation', type: 'decimal', precision: 12, scale: 2, insert: false, update: false })
  monthlyDepreciation?: number;

  // Fechas
  @Column({ name: 'depreciation_start_date', type: 'date' })
  depreciationStartDate!: Date;

  @Column({ name: 'depreciation_end_date', type: 'date', nullable: true })
  depreciationEndDate?: Date;

  // Estado actual
  @Column({ name: 'accumulated_depreciation', type: 'decimal', precision: 18, scale: 2, default: 0 })
  accumulatedDepreciation!: number;

  @Column({ name: 'current_book_value', type: 'decimal', precision: 18, scale: 2, nullable: true })
  currentBookValue?: number;

  @Column({ name: 'last_entry_date', type: 'date', nullable: true })
  lastEntryDate?: Date;

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

  @Column({ name: 'is_fully_depreciated', type: 'boolean', default: false })
  isFullyDepreciated!: boolean;

  // Notas y metadatos
  @Column({ type: 'text', nullable: true })
  notes?: string;

  @Column({ type: 'jsonb', nullable: true })
  metadata?: Record<string, any>;

  // Relaciones
  @OneToMany(() => DepreciationEntry, (entry) => entry.schedule)
  entries?: DepreciationEntry[];

  // Auditoria
  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy?: string;

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

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

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