import { api } from '@/services/api'
import type { Plan } from '@/modules/settings/services/planService'
import type { Company } from '@/modules/settings/services/companyService'

export interface SuperAdminSummary {
  companies: {
    total: number
    active: number
  }
  subscriptions: {
    trialing: number
    active: number
    past_due: number
    cancelled: number
  }
  plans: {
    total: number
    active: number
  }
  monthly_recurring_revenue: string
}

export interface Feature {
  id: number
  name: string
  slug: string
  module: string
  description: string | null
  is_active: boolean
  plans_count: number
}

export interface Subscription {
  id: number
  billing_cycle: string
  status: string
  starts_at: string | null
  trial_ends_at: string | null
  renews_at: string | null
  ends_at: string | null
  company: {
    id: number
    name: string
    slug: string
  }
  plan: {
    id: number
    name: string
    slug: string
  }
}

export interface PlanPayload {
  name: string
  slug?: string
  description?: string
  monthly_price: number
  yearly_price: number
  trial_days?: number
  max_users?: number | null
  max_branches?: number | null
  is_active?: boolean
  feature_ids?: number[]
}

interface Paginated<T> {
  data: T[]
}

export const superAdminService = {
  async summary() {
    const { data } = await api.get<{ data: SuperAdminSummary }>('/super-admin/summary')

    return data.data
  },

  async companies() {
    const { data } = await api.get<{ data: Paginated<Company> }>('/super-admin/companies')

    return data.data.data
  },

  async plans() {
    const { data } = await api.get<{ data: Plan[] }>('/super-admin/plans')

    return data.data
  },

  async features() {
    const { data } = await api.get<{ data: Feature[] }>('/super-admin/features')

    return data.data
  },

  async subscriptions() {
    const { data } = await api.get<{ data: Paginated<Subscription> }>('/super-admin/subscriptions')

    return data.data.data
  },

  async toggleFeature(feature: Feature) {
    const { data } = await api.patch<{ data: Feature }>(`/super-admin/features/${feature.id}`, {
      is_active: !feature.is_active,
    })

    return data.data
  },

  async updateFeature(feature: Feature, payload: Partial<Pick<Feature, 'name' | 'description' | 'is_active'>>) {
    const { data } = await api.patch<{ data: Feature }>(`/super-admin/features/${feature.id}`, payload)

    return data.data
  },

  async createPlan(payload: PlanPayload) {
    const { data } = await api.post<{ data: Plan }>('/super-admin/plans', payload)

    return data.data
  },

  async updatePlan(plan: Plan, payload: Partial<PlanPayload>) {
    const { data } = await api.patch<{ data: Plan }>(`/super-admin/plans/${plan.id}`, payload)

    return data.data
  },

  async updateCompanyStatus(company: Company, status: string) {
    const { data } = await api.patch<{ data: Company }>(`/super-admin/companies/${company.id}/status`, {
      status,
    })

    return data.data
  },

  async updateSubscriptionStatus(subscription: Subscription, status: string) {
    const { data } = await api.patch<{ data: Subscription }>(`/super-admin/subscriptions/${subscription.id}`, {
      status,
    })

    return data.data
  },

  async updateSubscription(subscription: Subscription, payload: Partial<Pick<Subscription, 'billing_cycle' | 'status' | 'renews_at' | 'ends_at'>> & { plan_id?: number }) {
    const { data } = await api.patch<{ data: Subscription }>(`/super-admin/subscriptions/${subscription.id}`, payload)

    return data.data
  },
}
