import {
  createApi,
  createLoggerMiddleware,
  createTransformerMiddleware,
} from "@simple-api/core";
import { authMiddleware, refreshMiddleware } from "./api-middlewares";

// Trailing slash is essential for relative path resolution
export const BASE_URL = "https://api.yaltopiaticket.com/";

/**
 * Central API client using simple-api
 */
export const api = createApi({
  baseUrl: BASE_URL,
  middleware: [
    createLoggerMiddleware(),
    createTransformerMiddleware(),
    refreshMiddleware,
  ],
  services: {
    notifications: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "notifications" },
        settings: { method: "GET", path: "notifications/settings" },
        update: { method: "PUT", path: "notifications/settings" },
      },
    },
    news: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "news" },
        getLatest: { method: "GET", path: "news/latest" },
        getById: { method: "GET", path: "news/:id" },
      },
    },
    auth: {
      middleware: [authMiddleware],
      endpoints: {
        login: { method: "POST", path: "auth/login" },
        register: { method: "POST", path: "auth/login-or-register-owner" },
        refresh: { method: "POST", path: "auth/refresh" },
        logout: { method: "POST", path: "auth/logout" },
        profile: { method: "GET", path: "auth/profile" },
        googleMobile: { method: "POST", path: "auth/google/mobile" },
        changePassword: { method: "POST", path: "auth/change-password" },
        sendOtp: { method: "POST", path: "auth/phone/otp/send" },
        resendOtp: { method: "POST", path: "auth/phone/otp/resend" },
        verifyOtp: { method: "POST", path: "auth/phone/otp/verify" },
        setPin: { method: "POST", path: "auth/set-pin" },
        verifyPin: { method: "GET", path: "auth/pin" },
        forgetPin: { method: "POST", path: "auth/forget-pin" },
        forgetPinVerify: { method: "POST", path: "auth/forget-pin/verify" },
      },
    },
    invoices: {
      middleware: [authMiddleware],
      endpoints: {
        stats: { method: "GET", path: "invoices/stats" },
        getAll: { method: "GET", path: "invoices" },
        getById: { method: "GET", path: "invoices/:id" },
        create: { method: "POST", path: "invoices" },
        update: { method: "PUT", path: "invoices/:id" },
        delete: { method: "DELETE", path: "invoices/:id" },
        getPdf: { method: "GET", path: "invoices/:id/pdf" },
        shareLink: { method: "POST", path: "invoices/share/link" },
      },
    },
    users: {
      middleware: [authMiddleware],
      endpoints: {
        me: { method: "GET", path: "users/me" },
        getAll: { method: "GET", path: "users" },
        updateProfile: { method: "PUT", path: "users/me" },
        create: { method: "POST", path: "auth/register" },
      },
    },
    company: {
      middleware: [authMiddleware],
      endpoints: {
        get: { method: "GET", path: "company" },
        update: { method: "PUT", path: "company" },
        paymentMethods: { method: "GET", path: "company/payment-methods" },
      },
    },
    team: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "team" },
        getById: { method: "GET", path: "team/:id" },
        create: { method: "POST", path: "team" },
      },
    },
    scan: {
      middleware: [authMiddleware],
      endpoints: {
        invoice: { method: "POST", path: "scan/invoice" },
        paymentReceipt: { method: "POST", path: "scan/payment-receipt" },
        verifyPaymentReference: {
          method: "POST",
          path: "scan/verify-payment-reference",
        },
        paymentReceiptManual: {
          method: "POST",
          path: "scan/payment-receipt/manual",
        },
      },
    },
    payments: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "payments" },
        getById: { method: "GET", path: "payments/:id" },
        create: { method: "POST", path: "payments" },
        update: { method: "PUT", path: "payments/:id" },
        associate: { method: "POST", path: "payments/:id/associate" },
        verifySms: { method: "POST", path: "payments/:id/verify-sms" },
        flag: { method: "POST", path: "payments/:id/flag" },
        delete: { method: "DELETE", path: "payments/:id" },
      },
    },
    paymentRequests: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "payment-requests" },
        getById: { method: "GET", path: "payment-requests/:id" },
        create: { method: "POST", path: "payment-requests" },
        update: { method: "PUT", path: "payment-requests/:id" },
        open: { method: "POST", path: "payment-requests/:id/open" },
        send: { method: "POST", path: "payment-requests/:id/send" },
      },
    },
    proforma: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "proforma" },
        getById: { method: "GET", path: "proforma/:id" },
        create: { method: "POST", path: "proforma" },
        update: { method: "PUT", path: "proforma/:id" },
        delete: { method: "DELETE", path: "proforma/:id" },
        getPdf: { method: "GET", path: "proforma/:id/pdf" },
        shareLink: { method: "POST", path: "proforma/share/link" },
      },
    },
    proformaRequests: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "proforma-requests" },
        getById: { method: "GET", path: "proforma-requests/:id" },
        create: { method: "POST", path: "proforma-requests" },
      },
    },
    rbac: {
      middleware: [authMiddleware],
      endpoints: {
        roles: { method: "GET", path: "rbac/roles" },
        permissions: { method: "GET", path: "rbac/permissions" },
      },
    },
    support: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "support" },
        create: { method: "POST", path: "support" },
      },
    },
    faq: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "faq" },
      },
    },
    customers: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "customers" },
        getById: { method: "GET", path: "customers/:id" },
        create: { method: "POST", path: "customers" },
        update: { method: "PUT", path: "customers/:id" },
        delete: { method: "DELETE", path: "customers/:id" },
      },
    },
    declarations: {
      middleware: [authMiddleware],
      endpoints: {
        getAll: { method: "GET", path: "declarations" },
        getById: { method: "GET", path: "declarations/:id" },
        create: { method: "POST", path: "declarations" },
        update: { method: "PUT", path: "declarations/:id" },
        delete: { method: "DELETE", path: "declarations/:id" },
        scan: { method: "POST", path: "declarations/scan" },
      },
    },
  },
});

export interface AuthResponse {
  accessToken: string;
  refreshToken: string;
  user: any;
}

// Explicit exports for convenience and to avoid undefined access
export const authApi = api.auth;
export const newsApi = api.news;
export const invoicesApi = api.invoices;
export const proformaApi = api.proforma;
export const rbacApi = api.rbac;
export const supportApi = api.support;
export const faqApi = api.faq;
