import { BaseEntity } from '../../base/entity/base.js';
import { Column, Entity, Index } from 'typeorm';

export interface NetaClawDataSourceExtra {
  ssl?: boolean | Record<string, unknown>;
  connectTimeout?: number;
  queryTimeoutMs?: number;
  maxRows?: number;
  allowedTables?: string[];
  blockedTables?: string[];
  maskedColumns?: Record<string, 'hash' | 'partial' | 'redact'>;
  schemaVisibility?: 'allowed-only' | 'all-names-only';
  maxJoinTables?: number;
  poolConnectionLimit?: number;
}

@Entity('netaclaw_data_source')
export class NetaClawDataSourceEntity extends BaseEntity {
  @Index({ unique: true })
  @Column({ comment: '唯一标识', length: 100 })
  name: string;

  @Column({ comment: '显示名称', length: 200, nullable: true })
  label: string | null;

  @Index()
  @Column({ comment: '数据源类型', length: 20, default: 'mysql' })
  type: string;

  @Column({ comment: '主机地址', length: 255 })
  host: string;

  @Column({ comment: '端口', default: 3306 })
  port: number;

  @Column({ comment: '数据库名', length: 128 })
  database: string;

  @Column({ comment: '用户名', length: 255 })
  username: string;

  @Column({ comment: '加密密码', type: 'text', nullable: true })
  passwordEncrypted: string | null;

  @Column({ comment: '是否只读', default: true })
  readonly: boolean;

  @Index()
  @Column({ comment: '状态 0=禁用 1=启用', default: 1 })
  status: number;

  @Column({ comment: '允许访问的Agent ID列表', type: 'json', nullable: true })
  allowedAgentIds: number[] | null;

  @Column({ comment: '扩展配置', type: 'json', nullable: true })
  extra: NetaClawDataSourceExtra | null;
}
