/**
 * 文件职责：员工排班模块 API 与 DTO 定义。
 * 1. 维护员工、班次模板、排班表类型。
 * 2. 提供查询、保存、删除、复制接口。
 */
import type { PaginatedResult } from '#/api/store';

import { requestClient } from '#/api/request';

/** 员工角色 */
export type StaffRoleType = 'cashier' | 'chef' | 'courier' | 'manager';

/** 员工状态 */
export type StaffStatus = 'active' | 'leave' | 'resigned';

/** 班次类型 */
export type ShiftType = 'evening' | 'full' | 'morning' | 'off';

/** 员工档案 */
export interface StoreStaffDto {
  avatarColor: string;
  email: string;
  hiredAt: string;
  id: string;
  name: string;
  permissions: string[];
  phone: string;
  roleType: StaffRoleType;
  status: StaffStatus;
}

/** 班次模板条目 */
export interface ShiftTemplateItemDto {
  endTime: string;
  startTime: string;
}

/** 门店班次模板 */
export interface StoreShiftTemplatesDto {
  evening: ShiftTemplateItemDto;
  full: ShiftTemplateItemDto;
  morning: ShiftTemplateItemDto;
}

/** 单日排班 */
export interface StaffDayShiftDto {
  dayOfWeek: number;
  endTime: string;
  shiftType: ShiftType;
  startTime: string;
}

/** 员工排班 */
export interface StaffScheduleDto {
  shifts: StaffDayShiftDto[];
  staffId: string;
}

/** 门店排班聚合 */
export interface StoreStaffScheduleDto {
  isScheduleConfigured: boolean;
  isTemplateConfigured: boolean;
  schedules: StaffScheduleDto[];
  storeId: string;
  templates: null | StoreShiftTemplatesDto;
  weekStartDate: string;
}

/** 员工列表查询参数 */
export interface StoreStaffListQuery {
  keyword?: string;
  page: number;
  pageSize: number;
  roleType?: StaffRoleType;
  status?: StaffStatus;
  storeId: string;
}

/** 保存员工参数 */
export interface SaveStoreStaffParams {
  email: string;
  id?: string;
  name: string;
  permissions: string[];
  phone: string;
  roleType: StaffRoleType;
  status: StaffStatus;
  storeId: string;
}

/** 删除员工参数 */
export interface DeleteStoreStaffParams {
  staffId: string;
  storeId: string;
}

/** 保存班次模板参数 */
export interface SaveStoreStaffTemplatesParams {
  storeId: string;
  templates: StoreShiftTemplatesDto;
}

/** 保存个人排班参数 */
export interface SaveStoreStaffPersonalScheduleParams {
  shifts: StaffDayShiftDto[];
  staffId: string;
  storeId: string;
}

/** 保存周排班参数 */
export interface SaveStoreStaffWeeklyScheduleParams {
  schedules: StaffScheduleDto[];
  storeId: string;
}

/** 复制排班参数 */
export interface CopyStoreStaffScheduleParams {
  copyScope: 'template_and_schedule';
  sourceStoreId: string;
  targetStoreIds: string[];
}

/** 获取员工列表 */
export async function getStoreStaffListApi(params: StoreStaffListQuery) {
  return requestClient.get<PaginatedResult<StoreStaffDto>>('/store/staff', {
    params,
  });
}

/** 新增/编辑员工 */
export async function saveStoreStaffApi(data: SaveStoreStaffParams) {
  return requestClient.post<StoreStaffDto>('/store/staff/save', data);
}

/** 删除员工 */
export async function deleteStoreStaffApi(data: DeleteStoreStaffParams) {
  return requestClient.post('/store/staff/delete', data);
}

/** 获取门店排班配置 */
export async function getStoreStaffScheduleApi(
  storeId: string,
  weekStartDate?: string,
) {
  return requestClient.get<StoreStaffScheduleDto>('/store/staff/schedule', {
    params: {
      storeId,
      weekStartDate,
    },
  });
}

/** 保存班次模板 */
export async function saveStoreStaffTemplatesApi(
  data: SaveStoreStaffTemplatesParams,
) {
  return requestClient.post<StoreShiftTemplatesDto>(
    '/store/staff/template/save',
    data,
  );
}

/** 保存单员工排班 */
export async function saveStoreStaffPersonalScheduleApi(
  data: SaveStoreStaffPersonalScheduleParams,
) {
  return requestClient.post<StaffScheduleDto>(
    '/store/staff/schedule/personal/save',
    data,
  );
}

/** 保存周排班 */
export async function saveStoreStaffWeeklyScheduleApi(
  data: SaveStoreStaffWeeklyScheduleParams,
) {
  return requestClient.post<StoreStaffScheduleDto>(
    '/store/staff/schedule/weekly/save',
    data,
  );
}

/** 复制班次模板与排班 */
export async function copyStoreStaffScheduleApi(
  data: CopyStoreStaffScheduleParams,
) {
  return requestClient.post('/store/staff/copy', data);
}
