import ApiService from './ApiService';

export interface CreateAffiliateData {
    name: string;
    email: string;
    address1: string;
    address2?: string;
    city: string;
    state: string;
    zip: string;
    is_active?: boolean;
}

export interface AffiliateResponse {
    id: number;
    name: string;
    email: string;
    address1: string;
    address2?: string;
    city: string;
    state: string;
    zip: string;
    is_active: boolean;
    created_at: string;
    updated_at: string;
    creator?: {
        id: number;
        first_name: string;
        last_name: string;
        email: string;
        fullname: string;
    };
}

export interface AffiliatesListResponse {
    error: boolean;
    message: string;
    code: number;
    results: {
        current_page: number;
        data: AffiliateResponse[];
        first_page_url: string;
        from: number;
        last_page: number;
        last_page_url: string;
        links: any[];
        next_page_url: string | null;
        path: string;
        per_page: number;
        prev_page_url: string | null;
        to: number;
        total: number;
    };
}

export async function apiCreateAffiliate(data: CreateAffiliateData) {
    return ApiService.request<{
        error: boolean;
        message: string;
        results: AffiliateResponse;
    }>({
        url: 'v1/affiliates',
        method: 'post',
        data,
    });
}

export interface GetAffiliatesParams {
    search?: string;
    per_page?: number;
    page?: number;
    sort_by?: string;
    sort_order?: 'asc' | 'desc';
}

export async function apiGetAffiliates(params?: GetAffiliatesParams) {
    return ApiService.request<AffiliatesListResponse>({
        url: 'v1/affiliates',
        method: 'get',
        params: params || {},
    });
}

export async function apiUpdateAffiliate(id: number, data: Partial<CreateAffiliateData>) {
    return ApiService.request<{
        error: boolean;
        message: string;
        code: number;
        results: AffiliateResponse;
    }>({
        url: `v1/affiliates/${id}`,
        method: 'put',
        data,
    });
}

export async function apiDeleteAffiliate(id: number) {
    return ApiService.request<{
        error: boolean;
        message: string;
        code: number;
    }>({
        url: `v1/affiliates/${id}`,
        method: 'delete',
    });
}

export async function apiUpdateAffiliateStatus(id: number, data: { is_active: boolean }) {
    return ApiService.request<{
        error: boolean;
        message: string;
        code: number;
        results: AffiliateResponse;
    }>({
        url: `v1/affiliate-status-update/${id}`,
        method: 'put',
        data,
    });
}
