/**
 * KpiSnapshot Entity
 * Snapshots históricos de KPIs para análisis
 *
 * @module Reports
 * @table reports.kpi_snapshots
 */

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

export type KpiCategory =
  | 'financial'
  | 'progress'
  | 'quality'
  | 'hse'
  | 'hr'
  | 'inventory'
  | 'sales'
  | 'operational';

export type KpiPeriodType = 'daily' | 'weekly' | 'monthly' | 'quarterly' | 'yearly';

export type TrendDirection = 'up' | 'down' | 'stable';

@Entity({ schema: 'reports', name: 'kpi_snapshots' })
@Index(['tenantId', 'kpiCode', 'snapshotDate'])
@Index(['tenantId'])
@Index(['kpiCode'])
@Index(['category'])
@Index(['snapshotDate'])
@Index(['fraccionamientoId'])
export class KpiSnapshot {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({
    name: 'kpi_code',
    type: 'varchar',
    length: 50,
    comment: 'Unique KPI identifier',
  })
  kpiCode: string;

  @Column({
    name: 'kpi_name',
    type: 'varchar',
    length: 200,
  })
  kpiName: string;

  @Column({
    type: 'varchar',
    length: 30,
  })
  category: KpiCategory;

  @Column({
    name: 'snapshot_date',
    type: 'date',
  })
  snapshotDate: Date;

  @Column({
    name: 'period_type',
    type: 'varchar',
    length: 20,
    default: 'daily',
  })
  periodType: KpiPeriodType;

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

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

  @Column({
    name: 'fraccionamiento_id',
    type: 'uuid',
    nullable: true,
    comment: 'Project-specific KPI, null for global',
  })
  fraccionamientoId: string | null;

  @Column({
    type: 'decimal',
    precision: 18,
    scale: 4,
  })
  value: number;

  @Column({
    name: 'previous_value',
    type: 'decimal',
    precision: 18,
    scale: 4,
    nullable: true,
  })
  previousValue: number | null;

  @Column({
    name: 'target_value',
    type: 'decimal',
    precision: 18,
    scale: 4,
    nullable: true,
  })
  targetValue: number | null;

  @Column({
    name: 'min_value',
    type: 'decimal',
    precision: 18,
    scale: 4,
    nullable: true,
    comment: 'Minimum acceptable value',
  })
  minValue: number | null;

  @Column({
    name: 'max_value',
    type: 'decimal',
    precision: 18,
    scale: 4,
    nullable: true,
    comment: 'Maximum acceptable value',
  })
  maxValue: number | null;

  @Column({
    type: 'varchar',
    length: 20,
    nullable: true,
    comment: 'Unit of measurement',
  })
  unit: string | null;

  @Column({
    name: 'change_percentage',
    type: 'decimal',
    precision: 8,
    scale: 2,
    nullable: true,
    comment: 'Percentage change from previous',
  })
  changePercentage: number | null;

  @Column({
    name: 'trend_direction',
    type: 'varchar',
    length: 10,
    nullable: true,
  })
  trendDirection: TrendDirection | null;

  @Column({
    name: 'is_on_target',
    type: 'boolean',
    nullable: true,
  })
  isOnTarget: boolean | null;

  @Column({
    name: 'status_color',
    type: 'varchar',
    length: 20,
    nullable: true,
    comment: 'green, yellow, red based on thresholds',
  })
  statusColor: string | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Additional breakdown data',
  })
  breakdown: Record<string, any> | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Source data references',
  })
  metadata: Record<string, any> | null;

  @Column({
    name: 'calculated_at',
    type: 'timestamptz',
    default: () => 'CURRENT_TIMESTAMP',
  })
  calculatedAt: Date;

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

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