/**
 * MaintenancePlan Entity - Planes de Mantenimiento
 *
 * Planes de mantenimiento preventivo para activos.
 *
 * @module Assets (MAE-015)
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Asset } from './asset.entity';
import { AssetCategory } from './asset-category.entity';

export type MaintenanceFrequency =
  | 'daily'
  | 'weekly'
  | 'biweekly'
  | 'monthly'
  | 'quarterly'
  | 'semiannual'
  | 'annual'
  | 'by_hours'
  | 'by_kilometers';

@Entity('maintenance_plans', { schema: 'assets' })
@Index(['tenantId', 'planCode'], { unique: true })
@Index(['tenantId', 'assetId'])
@Index(['tenantId', 'categoryId'])
export class MaintenancePlan {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Identificacion
  @Column({ name: 'plan_code', length: 50 })
  planCode!: string;

  @Column({ length: 255 })
  name!: string;

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

  // Aplica a
  @Column({ name: 'asset_id', type: 'uuid', nullable: true })
  assetId?: string;

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

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

  @ManyToOne(() => AssetCategory, { nullable: true })
  @JoinColumn({ name: 'category_id' })
  category?: AssetCategory;

  @Column({
    name: 'asset_type',
    type: 'enum',
    enum: ['heavy_machinery', 'light_equipment', 'vehicle', 'tool', 'computer', 'furniture', 'other'],
    enumName: 'asset_type',
    nullable: true,
  })
  assetType?: string;

  // Tipo y frecuencia
  @Column({
    name: 'maintenance_type',
    type: 'enum',
    enum: ['preventive', 'corrective', 'predictive', 'emergency'],
    enumName: 'maintenance_type',
    default: 'preventive',
  })
  maintenanceType!: string;

  @Column({
    type: 'enum',
    enum: ['daily', 'weekly', 'biweekly', 'monthly', 'quarterly', 'semiannual', 'annual', 'by_hours', 'by_kilometers'],
    enumName: 'maintenance_frequency',
  })
  frequency!: MaintenanceFrequency;

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

  // Actividades del plan
  @Column({ type: 'jsonb' })
  activities!: Record<string, any>[];

  // Duracion estimada
  @Column({ name: 'estimated_duration_hours', type: 'decimal', precision: 5, scale: 2, nullable: true })
  estimatedDurationHours?: number;

  // Recursos necesarios
  @Column({ name: 'required_parts', type: 'jsonb', nullable: true })
  requiredParts?: Record<string, any>[];

  @Column({ name: 'required_tools', type: 'jsonb', nullable: true })
  requiredTools?: Record<string, any>[];

  @Column({ name: 'required_skills', type: 'varchar', array: true, nullable: true })
  requiredSkills?: string[];

  // Costos estimados
  @Column({ name: 'estimated_labor_cost', type: 'decimal', precision: 18, scale: 2, nullable: true })
  estimatedLaborCost?: number;

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

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

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

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

  // 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;

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