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

export interface ProductResp {
  productId: number;
  companyId: number;
  companyName?: string;
  productCode: string;
  productName: string;
  insuranceType: string;
  productGroup?: string;
  payPeriodType?: string;
  isActive: string;
  launchDate?: string;
  endDate?: string;
}

export interface ProductSaveReq {
  productCode: string;
  productName: string;
  companyId: number;
  insuranceType: string;
  productGroup?: string;
  payPeriodType?: string;
  launchDate?: string;
  endDate?: string;
  isActive: string;
}

export interface ProductSearchParam {
  pageNum?: number;
  pageSize?: number;
  searchKeyword?: string;
  companyId?: number;
  insuranceType?: string;
  isActive?: string;
}

export const productApi = {
  list: (p: ProductSearchParam) =>
    unwrap<PageResponse<ProductResp>>(api.get('/api/products', { params: p })),
  create: (req: ProductSaveReq) =>
    unwrap<number>(api.post('/api/products', req)),
  update: (id: number, req: ProductSaveReq) =>
    unwrap<void>(api.put(`/api/products/${id}`, req)),
  remove: (id: number) =>
    unwrap<void>(api.delete(`/api/products/${id}`)),
};
