import request from '@/utils/request'
import type {
  SlotDefinition,
  SlotDefinitionCreateRequest,
  SlotDefinitionUpdateRequest,
  RuntimeSlotValue
} from '@/types/slot-definition'
import type { FieldRole } from '@/types/metadata'

export const slotDefinitionApi = {
  list: (required?: boolean) =>
    request<SlotDefinition[]>({
      method: 'GET',
      url: '/admin/slot-definitions',
      params: required !== undefined ? { required } : {}
    }),

  get: (id: string) =>
    request<SlotDefinition>({ method: 'GET', url: `/admin/slot-definitions/${id}` }),

  create: (data: SlotDefinitionCreateRequest) =>
    request<SlotDefinition>({ method: 'POST', url: '/admin/slot-definitions', data }),

  update: (id: string, data: SlotDefinitionUpdateRequest) =>
    request<SlotDefinition>({ method: 'PUT', url: `/admin/slot-definitions/${id}`, data }),

  delete: (id: string) =>
    request({ method: 'DELETE', url: `/admin/slot-definitions/${id}` }),

  getByRole: (role: FieldRole) =>
    request<SlotDefinition[]>({
      method: 'GET',
      url: '/mid/slots/by-role',
      params: { role }
    }),

  getSlotValue: (slotKey: string, userId?: string, sessionId?: string) =>
    request<RuntimeSlotValue>({
      method: 'GET',
      url: `/mid/slots/${slotKey}`,
      params: {
        ...(userId ? { user_id: userId } : {}),
        ...(sessionId ? { session_id: sessionId } : {})
      }
    })
}

export type {
  SlotDefinition,
  SlotDefinitionCreateRequest,
  SlotDefinitionUpdateRequest,
  RuntimeSlotValue
}
