/**
 * Report Entity
 * Definición de reportes configurables
 *
 * @module Reports
 * @table reports.report_definitions
 */

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

export type ReportType =
  | 'financial'
  | 'progress'
  | 'quality'
  | 'hse'
  | 'hr'
  | 'inventory'
  | 'contracts'
  | 'executive'
  | 'custom';

export type ReportFormat = 'pdf' | 'excel' | 'csv' | 'html' | 'json';

export type ReportFrequency = 'once' | 'daily' | 'weekly' | 'monthly' | 'quarterly' | 'yearly';

@Entity({ schema: 'reports', name: 'report_definitions' })
@Index(['tenantId', 'code'], { unique: true })
@Index(['tenantId'])
@Index(['reportType'])
@Index(['isActive'])
export class Report {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  @Column({
    name: 'report_type',
    type: 'varchar',
    length: 30,
  })
  reportType: ReportType;

  @Column({
    name: 'default_format',
    type: 'varchar',
    length: 20,
    default: 'pdf',
  })
  defaultFormat: ReportFormat;

  @Column({
    name: 'available_formats',
    type: 'varchar',
    array: true,
    default: ['pdf', 'excel'],
  })
  availableFormats: ReportFormat[];

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'SQL query or data source configuration',
  })
  query: Record<string, any> | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Report parameters definition',
  })
  parameters: Record<string, any> | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Column/field definitions',
  })
  columns: Record<string, any>[] | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Grouping and aggregation config',
  })
  grouping: Record<string, any> | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Sorting configuration',
  })
  sorting: Record<string, any>[] | null;

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

  @Column({
    name: 'template_path',
    type: 'varchar',
    length: 500,
    nullable: true,
  })
  templatePath: string | null;

  @Column({
    name: 'is_scheduled',
    type: 'boolean',
    default: false,
  })
  isScheduled: boolean;

  @Column({
    type: 'varchar',
    length: 20,
    nullable: true,
  })
  frequency: ReportFrequency | null;

  @Column({
    name: 'schedule_config',
    type: 'jsonb',
    nullable: true,
    comment: 'Cron or schedule configuration',
  })
  scheduleConfig: Record<string, any> | null;

  @Column({
    name: 'distribution_list',
    type: 'varchar',
    array: true,
    nullable: true,
    comment: 'Email addresses for distribution',
  })
  distributionList: string[] | null;

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

  @Column({
    name: 'is_system',
    type: 'boolean',
    default: false,
    comment: 'System report cannot be deleted',
  })
  isSystem: boolean;

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

  @Column({
    name: 'last_executed_at',
    type: 'timestamptz',
    nullable: true,
  })
  lastExecutedAt: Date | null;

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

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

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

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

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

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

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

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

  @OneToMany(() => ReportExecution, (e) => e.report)
  executions: ReportExecution[];
}
