import client from "./client"
import type { ApiResponse } from "../../types"
import { pageResult } from "./response"

export interface AdminMallClaimRecord {
  id: number
  user_id: number
  username: string
  nickname: string | null
  mall_item_id: number
  mall_item_title: string | null
  promo_code_id: number
  coupon_description: string | null
  coupon_type: string
  coupon_value: number
  points_paid: number
  status: string
  used_at: string | null
  order_id: number | null
  created_at: string | null
}

export interface AdminMallItem {
  id: number
  promo_code_id: number
  code: string
  title: string
  subtitle: string | null
  cover_image: string | null
  description: string | null
  category: string | null
  points_price: number
  stock_total: number
  stock_remaining: number
  status: string
  scope: string
  sort_order: number
  max_claims_per_user: number
  max_claims_per_user_per_day: number
  required_member_level: number
  required_member_level_name: string | null
  is_hot: boolean
  type: string
  value: number
  min_order_amount: number
  expires_at: string | null
  issuer_name: string
  created_at: string
}

export interface CreateMallCouponPayload {
  code?: string
  title: string
  subtitle?: string
  cover_image?: string
  description?: string
  category?: string
  points_price: number
  stock_total: number
  sort_order?: number
  max_claims_per_user?: number
  max_claims_per_user_per_day?: number
  required_member_level?: number
  required_member_level_name?: string
  is_hot?: boolean
  type: "fixed" | "threshold_fixed" | "percent"
  value: number
  threshold_amount?: number
  max_discount_amount?: number
  max_uses_per_user?: number
  min_order_amount?: number
  expires_at?: string | null
  new_users_only?: boolean
  scope?: "public" | "agent_subordinates"
}

export interface UpdateMallItemPayload {
  title?: string
  subtitle?: string
  cover_image?: string
  description?: string
  category?: string
  points_price?: number
  stock_total?: number
  stock_remaining?: number
  sort_order?: number
  max_claims_per_user?: number
  max_claims_per_user_per_day?: number
  required_member_level?: number
  required_member_level_name?: string
  is_hot?: boolean
  status?: "active" | "paused" | "expired"
  scope?: "public" | "agent_subordinates" | "specific_users"
}

export async function getAdminMallItems(params?: { status?: string; category?: string; limit?: number; offset?: number }) {
  const res = await client.get<ApiResponse<{ items: AdminMallItem[]; total: number } | AdminMallItem[]>>("/v1/admin/mall/items", { params })
  return pageResult<AdminMallItem>(res.data.data)
}

export async function createAdminMallCoupon(payload: CreateMallCouponPayload) {
  const res = await client.post<ApiResponse<{ id: number; promo_code_id: number; code: string; title: string; stock_total: number; points_price: number }>>("/v1/admin/mall/items", payload)
  return res.data.data
}

export async function updateAdminMallItem(itemId: number, payload: UpdateMallItemPayload) {
  const res = await client.put<ApiResponse<{ id: number; status: string }>>(`/v1/admin/mall/items/${itemId}`, payload)
  return res.data.data
}

export async function deleteAdminMallItem(itemId: number) {
  const res = await client.delete<ApiResponse<{ id: number }>>(`/v1/admin/mall/items/${itemId}`)
  return res.data.data
}

export async function getAdminMallClaims(params?: { status?: string; mall_item_id?: number; limit?: number; offset?: number }) {
  const res = await client.get<ApiResponse<{ items: AdminMallClaimRecord[]; total: number } | AdminMallClaimRecord[]>>("/v1/admin/mall/claims", { params })
  return pageResult<AdminMallClaimRecord>(res.data.data)
}
