// File: src/modules/json-schema/entities/json-schema.entity.ts
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
} from 'typeorm';

export interface VirtualColumnConfig {
  jsonPath: string;
  columnName: string;
  dataType: 'INT' | 'VARCHAR' | 'BOOLEAN' | 'DATE' | 'DECIMAL' | 'DATETIME';
  indexType?: 'INDEX' | 'UNIQUE' | 'FULLTEXT';
  isRequired: boolean;
}

@Entity('json_schemas')
@Index(['schemaCode', 'version'], { unique: true })
export class JsonSchema {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ name: 'schema_code', length: 100 })
  schemaCode!: string;

  @Column({ default: 1 })
  version!: number;

  @Column({ name: 'table_name', length: 100, nullable: false }) // ✅ เพิ่ม: ระบุตารางเป้าหมาย
  tableName!: string;

  @Column({ name: 'schema_definition', type: 'json' })
  schemaDefinition!: Record<string, unknown>;

  @Column({ name: 'ui_schema', type: 'json', nullable: true })
  uiSchema?: Record<string, unknown>;

  @Column({ name: 'virtual_columns', type: 'json', nullable: true })
  virtualColumns?: VirtualColumnConfig[];

  @Column({ name: 'migration_script', type: 'json', nullable: true })
  migrationScript?: Record<string, unknown>;

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

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

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