import api, { PageResponse, unwrap } from './request';

// ─── 타입 ────────────────────────────────────────────────────────────────────

export interface DataDomainResp {
  domainCode: string;
  domainName: string;
  domainCategory: string;
  dataType: string;
  length?: number;
  precision?: number;
  scale?: number;
  formatPattern?: string;
  validationRegex?: string;
  exampleValue?: string;
  maskingType?: string;
  encrypted: string; // 'Y'/'N'
  description?: string;
  isActive: string;
}

export interface DataDomainOption {
  domainCode: string;
  domainName: string;
  domainCategory: string;
}

export interface DataDomainSaveReq {
  domainCode: string;
  domainName: string;
  domainCategory: string;
  dataType: string;
  length?: number;
  precision?: number;
  scale?: number;
  formatPattern?: string;
  validationRegex?: string;
  exampleValue?: string;
  maskingType?: string;
  encrypted?: string;
  description?: string;
  isActive?: string;
}

export interface DataDomainQuery {
  domainCategory?: string;
  isActive?: string;
  searchKeyword?: string;
  pageNum?: number;
  pageSize?: number;
}

export interface DataDictionaryResp {
  dictId: number;
  tableName: string;
  columnName: string;
  domainCode?: string;
  domainName?: string;
  domainCategory?: string;
  columnLabel?: string;
  description?: string;
  isPk: string;
  isFk: string;
  fkReference?: string;
  isRequired: string;
  defaultValue?: string;
  businessRule?: string;
  sampleValue?: string;
  maskingType?: string;
  encrypted?: string;
}

export interface DataDictionaryFullResp {
  tableName: string;
  columnName: string;
  ordinalPosition: number;
  dataType: string;
  maxLength?: number;
  numPrecision?: number;
  numScale?: number;
  isNullable: string;
  columnDefault?: string;
  tableComment?: string;
  columnComment?: string;
  columnLabel?: string;
  dictDescription?: string;
  domainCode?: string;
  domainName?: string;
  domainCategory?: string;
  formatPattern?: string;
  maskingType?: string;
  encrypted?: string;
  isPk?: string;
  isFk?: string;
  fkReference?: string;
  businessRule?: string;
  sampleValue?: string;
}

export interface DataDictionarySaveReq {
  tableName: string;
  columnName: string;
  columnLabel?: string;
  description?: string;
  domainCode?: string;
  isPk?: string;
  isFk?: string;
  fkReference?: string;
  isRequired?: string;
  defaultValue?: string;
  businessRule?: string;
  sampleValue?: string;
}

export interface DataDictionaryQuery {
  tableName?: string;
  domainCode?: string;
  pageNum?: number;
  pageSize?: number;
}

// ─── API ─────────────────────────────────────────────────────────────────────

export const dataDomainApi = {
  list: (params: DataDomainQuery) =>
    unwrap<PageResponse<DataDomainResp>>(api.get('/api/data-domains', { params })),

  options: () =>
    unwrap<DataDomainOption[]>(api.get('/api/data-domains/options')),

  detail: (domainCode: string) =>
    unwrap<DataDomainResp>(api.get(`/api/data-domains/${domainCode}`)),

  create: (req: DataDomainSaveReq) =>
    unwrap<void>(api.post('/api/data-domains', req)),

  update: (domainCode: string, req: DataDomainSaveReq) =>
    unwrap<void>(api.put(`/api/data-domains/${domainCode}`, req)),

  remove: (domainCode: string) =>
    unwrap<void>(api.delete(`/api/data-domains/${domainCode}`)),
};

export const dataDictApi = {
  list: (params: DataDictionaryQuery) =>
    unwrap<PageResponse<DataDictionaryResp>>(api.get('/api/data-dictionary', { params })),

  tables: () =>
    unwrap<string[]>(api.get('/api/data-dictionary/tables')),

  full: (tableName: string) =>
    unwrap<DataDictionaryFullResp[]>(api.get('/api/data-dictionary/full', { params: { tableName } })),

  detail: (dictId: number) =>
    unwrap<DataDictionaryResp>(api.get(`/api/data-dictionary/${dictId}`)),

  create: (req: DataDictionarySaveReq) =>
    unwrap<void>(api.post('/api/data-dictionary', req)),

  update: (dictId: number, req: DataDictionarySaveReq) =>
    unwrap<void>(api.put(`/api/data-dictionary/${dictId}`, req)),

  remove: (dictId: number) =>
    unwrap<void>(api.delete(`/api/data-dictionary/${dictId}`)),
};
