import { apiClient } from './apiClient'

// ================== USERS ==================
export interface User {
    id: string
    company_id: string
    role: string
    name: string
    username: string
    email: string
    email_verified: boolean
    created_at: string
    updated_at: string
}

export interface UserPage {
    users: User[]
    total: number
    page: number
    page_size: number
}

export interface CreateUserRequest {
    company_id: string
    role: string
    name: string
    username: string
    email: string
    password: string
}

export interface UpdateUserRequest {
    company_id?: string
    role?: string
    name?: string
    username?: string
    email?: string
    password?: string
}

// ================== COMPANIES ==================
export interface Company {
    id: string
    cnpj: string
    corporate_name: string
    category: string
    license_number: string
    is_verified: boolean
    latitude: number
    longitude: number
    city: string
    state: string
    created_at: string
    updated_at: string
}

export interface CompanyPage {
    tenants: Company[]
    total: number
    page: number
    page_size: number
}

export interface CreateCompanyRequest {
    cnpj: string
    corporate_name: string
    category: string
    license_number: string
    latitude: number
    longitude: number
    city: string
    state: string
}

export interface UpdateCompanyRequest {
    cnpj?: string
    corporate_name?: string
    category?: string
    license_number?: string
    is_verified?: boolean
    latitude?: number
    longitude?: number
    city?: string
    state?: string
}

// ================== PRODUCTS ==================
export interface Product {
    id: string
    seller_id: string
    name: string
    description: string
    batch: string
    expires_at: string
    price_cents: number
    stock: number
    created_at: string
    updated_at: string
}

export interface ProductPage {
    products: Product[]
    total: number
    page: number
    page_size: number
}

export interface CreateProductRequest {
    seller_id: string
    name: string
    description: string
    batch: string
    expires_at: string
    price_cents: number
    stock: number
}

export interface UpdateProductRequest {
    name?: string
    description?: string
    batch?: string
    expires_at?: string
    price_cents?: number
    stock?: number
}

// ================== ORDERS ==================
export interface OrderItem {
    id: string
    order_id: string
    product_id: string
    quantity: number
    unit_cents: number
    batch: string
    expires_at: string
}

export interface Order {
    id: string
    buyer_id: string
    seller_id: string
    status: string
    total_cents: number
    items: OrderItem[]
    created_at: string
    updated_at: string
}

export interface OrderPage {
    orders: Order[]
    total: number
    page: number
    page_size: number
}

// ================== ADMIN SERVICE ==================
const log = (method: string, ...args: unknown[]) => {
    console.log(`📋 [adminService.${method}]`, ...args)
}

export const adminService = {
    // Users
    listUsers: async (page = 1, pageSize = 20) => {
        log('listUsers', { page, pageSize })
        const result = await apiClient.get<UserPage>(`/v1/users?page=${page}&page_size=${pageSize}`)
        log('listUsers result', result)
        return result
    },

    getUser: async (id: string) => {
        log('getUser', { id })
        const result = await apiClient.get<User>(`/v1/users/${id}`)
        log('getUser result', result)
        return result
    },

    createUser: async (data: CreateUserRequest) => {
        log('createUser', data)
        const result = await apiClient.post<User>('/v1/users', data)
        log('createUser result', result)
        return result
    },

    updateUser: async (id: string, data: UpdateUserRequest) => {
        log('updateUser', { id, data })
        const result = await apiClient.put<User>(`/v1/users/${id}`, data)
        log('updateUser result', result)
        return result
    },

    deleteUser: async (id: string) => {
        log('deleteUser', { id })
        const result = await apiClient.delete(`/v1/users/${id}`)
        log('deleteUser done')
        return result
    },

    // Companies
    listCompanies: async (page = 1, pageSize = 20) => {
        log('listCompanies', { page, pageSize })
        const result = await apiClient.get<CompanyPage>(`/v1/companies?page=${page}&page_size=${pageSize}`)
        log('listCompanies result', result)
        return result
    },

    getCompany: async (id: string) => {
        log('getCompany', { id })
        const result = await apiClient.get<Company>(`/v1/companies/${id}`)
        log('getCompany result', result)
        return result
    },

    createCompany: async (data: CreateCompanyRequest) => {
        log('createCompany', data)
        const result = await apiClient.post<Company>('/v1/companies', data)
        log('createCompany result', result)
        return result
    },

    updateCompany: async (id: string, data: UpdateCompanyRequest) => {
        log('updateCompany', { id, data })
        const result = await apiClient.patch<Company>(`/v1/companies/${id}`, data)
        log('updateCompany result', result)
        return result
    },

    deleteCompany: async (id: string) => {
        log('deleteCompany', { id })
        const result = await apiClient.delete(`/v1/companies/${id}`)
        log('deleteCompany done')
        return result
    },

    verifyCompany: async (id: string) => {
        log('verifyCompany', { id })
        const result = await apiClient.patch<Company>(`/v1/companies/${id}/verify`, {})
        log('verifyCompany result', result)
        return result
    },

    // Products
    listProducts: async (page = 1, pageSize = 20) => {
        log('listProducts', { page, pageSize })
        const result = await apiClient.get<ProductPage>(`/v1/products?page=${page}&page_size=${pageSize}`)
        log('listProducts result', result)
        return result
    },

    getProduct: async (id: string) => {
        log('getProduct', { id })
        const result = await apiClient.get<Product>(`/v1/products/${id}`)
        log('getProduct result', result)
        return result
    },

    createProduct: async (data: CreateProductRequest) => {
        log('createProduct', data)
        const result = await apiClient.post<Product>('/v1/products', data)
        log('createProduct result', result)
        return result
    },

    updateProduct: async (id: string, data: UpdateProductRequest) => {
        log('updateProduct', { id, data })
        const result = await apiClient.patch<Product>(`/v1/products/${id}`, data)
        log('updateProduct result', result)
        return result
    },

    deleteProduct: async (id: string) => {
        log('deleteProduct', { id })
        const result = await apiClient.delete(`/v1/products/${id}`)
        log('deleteProduct done')
        return result
    },

    // Orders
    listOrders: async (page = 1, pageSize = 20) => {
        log('listOrders', { page, pageSize })
        const result = await apiClient.get<OrderPage>(`/v1/orders?page=${page}&page_size=${pageSize}`)
        log('listOrders result', result)
        return result
    },

    getOrder: async (id: string) => {
        log('getOrder', { id })
        const result = await apiClient.get<Order>(`/v1/orders/${id}`)
        log('getOrder result', result)
        return result
    },

    updateOrderStatus: async (id: string, status: string) => {
        log('updateOrderStatus', { id, status })
        const result = await apiClient.patch(`/v1/orders/${id}/status`, { status })
        log('updateOrderStatus done')
        return result
    },

    deleteOrder: async (id: string) => {
        log('deleteOrder', { id })
        const result = await apiClient.delete(`/v1/orders/${id}`)
        log('deleteOrder done')
        return result
    },

    // ================== REVIEWS ==================
    listReviews: async (page = 1, pageSize = 20) => {
        log('listReviews', { page, pageSize })
        const result = await apiClient.get<ReviewPage>(`/v1/reviews?page=${page}&page_size=${pageSize}`)
        log('listReviews result', result)
        return result
    },

    // ================== SHIPMENTS ==================
    listShipments: async (page = 1, pageSize = 20) => {
        log('listShipments', { page, pageSize })
        const result = await apiClient.get<ShipmentPage>(`/v1/shipments?page=${page}&page_size=${pageSize}`)
        log('listShipments result', result)
        return result
    },

    // ================== SHIPPING SETTINGS ==================
    getShippingSettings: async (vendorID: string) => {
        log('getShippingSettings', { vendorID })
        const result = await apiClient.get<ShippingSettings>(`/v1/shipping/settings/${vendorID}`)
        log('getShippingSettings result', result)
        return result
    },

    upsertShippingSettings: async (vendorID: string, data: ShippingSettings) => {
        log('upsertShippingSettings', { vendorID, data })
        const result = await apiClient.put<ShippingSettings>(`/v1/shipping/settings/${vendorID}`, data)
        log('upsertShippingSettings result', result)
        return result
    },
}

// ================== REVIEWS & SHIPMENTS TYPES ==================
export interface Review {
    id: string
    order_id: string
    buyer_id: string
    seller_id: string
    rating: number
    comment: string
    created_at: string
}

export interface ReviewPage {
    reviews: Review[]
    total: number
    page: number
    page_size: number
}

export interface Shipment {
    id: string
    order_id: string
    carrier: string
    tracking_code: string
    external_tracking: string
    status: string
    created_at: string
    updated_at: string
}

export interface ShipmentPage {
    shipments: Shipment[]
    total: number
    page: number
    page_size: number
}

// ================== SHIPPING SETTINGS ==================
export interface ShippingSettings {
    vendor_id?: string
    active: boolean
    max_radius_km: number
    price_per_km_cents: number
    min_fee_cents: number
    free_shipping_threshold_cents?: number
    pickup_active: boolean
    pickup_address?: string
    pickup_hours?: string
    latitude: number
    longitude: number
}
