import type { ApiResponse } from "../../types"
import client from "../../api/client"
import adminClient from "./client"

export type ActivityStatus = "draft" | "ongoing" | "upcoming" | "ended"
export type ActivityTemplate = "standard" | "product_grid" | "flash_sale" | "coupon"
export type ParticipationMode = "direct" | "existing_discount" | "promo_code" | "activity_price"
export type ActivityAudience = "all" | "new_user" | "member" | "agent"
export type ActivityCardStyle = "a" | "b" | "c" | "d"

export interface ActivityPricing {
  id: number
  cycle: string
  price: number
  original_price: number | null
  setup_fee: number
  is_enabled: boolean
  sort_order: number
}

export interface ActivityProduct {
  id?: number
  product_id: number
  name?: string
  sku?: string
  status?: string
  section_name: string
  section_description?: string | null
  card_style: ActivityCardStyle
  custom_label?: string | null
  product_description?: string | null
  billing_cycle?: string | null
  activity_price?: number | null
  discount_percent?: number | null
  stock_limit: number
  per_user_limit: number
  sort_order: number
  stock?: number
  stock_policy?: string
  vcpu?: number | null
  memory?: string | null
  disk?: string | null
  bandwidth?: string | null
  features?: string | null
  badges?: string | null
  group_name?: string
  category_name?: string
  store_url?: string
  pricing?: ActivityPricing | null
  pricings?: ActivityPricing[]
}

export interface ActivityPromoCode {
  id: number
  code: string
  description: string | null
  type: string
  value: number
}

export interface AdminActivity {
  id: number
  title: string
  subtitle: string | null
  description: string
  content: string | null
  image_url: string | null
  banner_url: string | null
  target_url: string | null
  label: string | null
  template_key: ActivityTemplate
  button_text: string
  participation_mode: ParticipationMode
  promo_code_id: number | null
  promo_code?: ActivityPromoCode | null
  audience: ActivityAudience
  rules: string | null
  per_user_limit: number
  total_limit: number
  limited_time: boolean
  starts_at: string | null
  ends_at: string | null
  is_published: boolean
  is_featured: boolean
  show_on_home: boolean
  sort_order: number
  created_by: number | null
  status: ActivityStatus
  status_text: string
  detail_url: string
  products?: ActivityProduct[]
  created_at: string
  updated_at: string
}

export interface ActivityPayload extends Partial<Omit<AdminActivity, "id" | "status" | "status_text" | "detail_url" | "created_at" | "updated_at" | "created_by" | "promo_code">> {
  products?: ActivityProduct[]
  replace_products?: boolean
}

export interface ActivityList {
  items: AdminActivity[]
  total: number
}

export interface ActivityProductOption {
  id: number
  name: string
  sku: string
  status: string
  group_name: string
  category_name: string
}

export interface ActivityPromoOption {
  id: number
  code: string
  description: string | null
  type: string
  value: number
  is_enabled: boolean
  expires_at: string | null
}

export interface ActivityOptions {
  products: ActivityProductOption[]
  promo_codes: ActivityPromoOption[]
}

const ADMIN_BASE = "/v1/admin/activities"
const PUBLIC_BASE = "/v1/activities"

export async function listActivities(params?: { keyword?: string; published?: boolean | "all"; offset?: number; limit?: number }) {
  const query = new URLSearchParams()
  if (params?.keyword) query.set("keyword", params.keyword)
  if (params?.published !== undefined && params.published !== "all") query.set("published", String(params.published))
  query.set("offset", String(params?.offset ?? 0))
  query.set("limit", String(params?.limit ?? 20))
  const { data } = await adminClient.get<ApiResponse<ActivityList>>(`${ADMIN_BASE}?${query.toString()}`)
  return data.data ?? { items: [], total: 0 }
}

export async function getActivityOptions() {
  const { data } = await adminClient.get<ApiResponse<ActivityOptions>>(`${ADMIN_BASE}/options`)
  return data.data ?? { products: [], promo_codes: [] }
}

export async function createActivity(payload: ActivityPayload) {
  const { data } = await adminClient.post<ApiResponse<AdminActivity>>(ADMIN_BASE, payload)
  return data.data
}

export async function updateActivity(id: number, payload: ActivityPayload) {
  const { data } = await adminClient.put<ApiResponse<AdminActivity>>(`${ADMIN_BASE}/${id}`, payload)
  return data.data
}

export async function deleteActivity(id: number) {
  const { data } = await adminClient.delete<ApiResponse<null>>(`${ADMIN_BASE}/${id}`)
  return data
}

export async function fetchPublicActivities(status: "all" | "ongoing" | "upcoming" | "ended" = "all") {
  const { data } = await client.get<ApiResponse<AdminActivity[]>>(`${PUBLIC_BASE}?status=${status}`)
  return data.data ?? []
}

export async function fetchPublicActivity(id: number) {
  const { data } = await client.get<ApiResponse<AdminActivity>>(`${PUBLIC_BASE}/${id}`)
  return data.data
}
