import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  OneToMany,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { ReportExecution } from './report-execution.entity';
import { ReportSchedule } from './report-schedule.entity';
import { CustomReport } from './custom-report.entity';

/**
 * Report type enum
 */
export enum ReportType {
  FINANCIAL = 'financial',
  ACCOUNTING = 'accounting',
  TAX = 'tax',
  MANAGEMENT = 'management',
  OPERATIONAL = 'operational',
  CUSTOM = 'custom',
}

/**
 * Report Definition Entity (schema: reports.report_definitions)
 *
 * Defines the structure and configuration of reports.
 * Contains the base query, parameters schema, and column configuration.
 */
@Entity({ name: 'report_definitions', schema: 'reports' })
@Index(['tenantId', 'code'], { unique: true })
export class ReportDefinition {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

  @Index()
  @Column({ name: 'category', type: 'varchar', length: 100, nullable: true })
  category: string | null;

  @Index()
  @Column({
    name: 'report_type',
    type: 'enum',
    enum: ReportType,
    enumName: 'report_type',
    default: ReportType.CUSTOM,
  })
  reportType: ReportType;

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

  @Column({ name: 'query_function', type: 'varchar', length: 255, nullable: true })
  queryFunction: string | null;

  @Column({ name: 'is_sql_based', type: 'boolean', default: true })
  isSqlBased: boolean;

  @Column({ name: 'parameters_schema', type: 'jsonb', default: '[]' })
  parametersSchema: Record<string, any>[];

  @Column({ name: 'default_parameters', type: 'jsonb', default: '{}' })
  defaultParameters: Record<string, any>;

  @Column({ name: 'columns_config', type: 'jsonb', default: '[]' })
  columnsConfig: Record<string, any>[];

  @Column({ name: 'totals_config', type: 'jsonb', default: '[]' })
  totalsConfig: Record<string, any>[];

  @Column({ name: 'required_permissions', type: 'text', array: true, default: '{}' })
  requiredPermissions: string[];

  @Column({ name: 'is_public', type: 'boolean', default: false })
  isPublic: boolean;

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

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

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

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

  // Relations
  @OneToMany(() => ReportExecution, (execution) => execution.reportDefinition)
  executions: ReportExecution[];

  @OneToMany(() => ReportSchedule, (schedule) => schedule.reportDefinition)
  schedules: ReportSchedule[];

  @OneToMany(() => CustomReport, (customReport) => customReport.baseDefinition)
  customReports: CustomReport[];
}
