/**
 * SystemSetting Entity
 * Configuración del sistema por tenant
 *
 * @module Admin
 * @table admin.system_settings
 */

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

export type SettingCategory =
  | 'general'
  | 'security'
  | 'notifications'
  | 'integrations'
  | 'workflow'
  | 'reports'
  | 'backups'
  | 'appearance';

export type SettingDataType = 'string' | 'number' | 'boolean' | 'json' | 'array';

@Entity({ schema: 'admin', name: 'system_settings' })
@Index(['tenantId', 'key'], { unique: true })
@Index(['tenantId'])
@Index(['category'])
@Index(['isPublic'])
export class SystemSetting {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({
    type: 'varchar',
    length: 100,
    comment: 'Unique key for the setting',
  })
  key: string;

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

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

  @Column({
    type: 'varchar',
    length: 30,
    default: 'general',
  })
  category: SettingCategory;

  @Column({
    name: 'data_type',
    type: 'varchar',
    length: 20,
    default: 'string',
  })
  dataType: SettingDataType;

  @Column({
    type: 'text',
    comment: 'Current value (stored as string)',
  })
  value: string;

  @Column({
    name: 'default_value',
    type: 'text',
    nullable: true,
    comment: 'Default value for reset',
  })
  defaultValue: string | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Validation rules (min, max, pattern, options, etc.)',
  })
  validation: Record<string, any> | null;

  @Column({
    type: 'jsonb',
    nullable: true,
    comment: 'Allowed options for select/enum types',
  })
  options: Record<string, any>[] | null;

  @Column({
    name: 'is_public',
    type: 'boolean',
    default: false,
    comment: 'Can be read without authentication',
  })
  isPublic: boolean;

  @Column({
    name: 'is_encrypted',
    type: 'boolean',
    default: false,
    comment: 'Value is encrypted (for sensitive data)',
  })
  isEncrypted: boolean;

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

  @Column({
    name: 'requires_restart',
    type: 'boolean',
    default: false,
    comment: 'Requires app restart to take effect',
  })
  requiresRestart: boolean;

  @Column({
    name: 'allowed_roles',
    type: 'varchar',
    array: true,
    nullable: true,
    comment: 'Roles that can modify this setting',
  })
  allowedRoles: string[] | null;

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

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

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

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

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