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

// ── Response VO ────────────────────────────────────────────────────────────
export interface PeriodCloseResp {
  closeId: number;
  closeMonth: string;           // YYYYMM
  closedAt?: string;
  closedByName?: string;
  reopenAt?: string;
  reopenByName?: string;
  reopenReason?: string;
  note?: string;
  closeStatus: 'CLOSED' | 'REOPENED';
  createdAt: string;
}

// ── Request VOs ────────────────────────────────────────────────────────────
export interface PeriodCloseCreateReq {
  closeMonth: string;           // YYYYMM
  note?: string;
}

export interface PeriodReopenReq {
  reopenReason: string;
}

// ── Search Params ──────────────────────────────────────────────────────────
export interface PeriodCloseSearchParam {
  settleMonth?: string;
  closeStatus?: string;
  pageNum?: number;
  pageSize?: number;
}

// ── API ────────────────────────────────────────────────────────────────────
export const periodCloseApi = {
  list: (p: PeriodCloseSearchParam) =>
    unwrap<PageResponse<PeriodCloseResp>>(api.get('/api/period-close', { params: p })),

  get: (closeId: number) =>
    unwrap<PeriodCloseResp>(api.get(`/api/period-close/${closeId}`)),

  close: (req: PeriodCloseCreateReq) =>
    unwrap<number>(api.post('/api/period-close', req)),

  reopen: (closeId: number, req: PeriodReopenReq) =>
    unwrap<void>(api.post(`/api/period-close/${closeId}/reopen`, req)),
};
