import { del, post, put, request, setAdminAuthToken } from '../httpClient.ts';
import {
  adminLoginPayloadSchema,
  adminLoginResponseSchema,
  adminProfileSchema,
  adminServiceCategoryListSchema,
  adminServiceCategoryCreatePayloadSchema,
  adminServiceCategoryCreateResponseSchema,
  adminServiceCategoryUpdatePayloadSchema,
  adminServiceCategoryUpdateResponseSchema,
  adminNewsUpdatePayloadSchema,
  adminNewsUpdateResponseSchema,
  adminNewsCreatePayloadSchema,
  adminNewsCreateResponseSchema,
  adminRegisterPayloadSchema,
  adminRegisterResponseSchema,
  adminPasswordChangePayloadSchema,
  adminPasswordChangeResponseSchema,
  adminDeleteResponseSchema,
  adminServiceCreatePayloadSchema,
  adminServiceCreateResponseSchema,
  adminServiceUpdatePayloadSchema,
  adminServiceUpdateResponseSchema,
  type AdminLoginPayload,
  type AdminLoginResponse,
  type AdminProfile,
  type AdminServiceCategoryList,
  type AdminServiceCategoryCreatePayload,
  type AdminServiceCategoryCreateResponse,
  type AdminServiceCategoryUpdatePayload,
  type AdminServiceCategoryUpdateResponse,
  type AdminNewsUpdatePayload,
  type AdminNewsUpdateResponse,
  type AdminNewsCreatePayload,
  type AdminNewsCreateResponse,
  type AdminRegisterPayload,
  type AdminRegisterResponse,
  type AdminPasswordChangePayload,
  type AdminPasswordChangeResponse,
  type AdminDeleteResponse,
  type AdminServiceCreatePayload,
  type AdminServiceCreateResponse,
  type AdminServiceUpdatePayload,
  type AdminServiceUpdateResponse,
} from './types.ts';
import { type NewsPageResponse, newsPageSchema } from '../news/types.ts';
import type { ListNewsParams } from '../news';
import { serviceItemSchema, servicePageSchema } from '../services/types.ts';
import type { ServiceItem } from '../services/types.ts';
import type { ListServicesParams } from '../services';

const ADMIN_LOGIN_ENDPOINT = '/api/v1/admin/login';
const ADMIN_PROFILE_ENDPOINT = '/api/v1/admin';
const ADMIN_SERVICE_CATEGORIES_ENDPOINT = '/api/v1/admin/service-categories';
const ADMIN_SERVICES_ENDPOINT = '/api/v1/admin/services';
const ADMIN_NEWS_ENDPOINT = '/api/v1/admin/news';

const buildNewsQuery = (params?: ListNewsParams) => {
  if (!params) {
    return undefined;
  }
  const { page, limit, search, tags } = params;
  return { page, limit, search, tags };
};

const buildServicesQuery = (params?: ListServicesParams) => {
  if (!params) {
    return undefined;
  }
  const { page, limit, q, category, minPrice, maxPrice } = params;
  return { page, limit, q, category, minPrice, maxPrice };
};

export const adminApi = {
  async login(payload: AdminLoginPayload): Promise<AdminLoginResponse> {
    adminLoginPayloadSchema.parse(payload);
    const response = await post(ADMIN_LOGIN_ENDPOINT, adminLoginResponseSchema, {
      body: payload,
    });

    setAdminAuthToken(response.token);
    return response;
  },

  async profile(): Promise<AdminProfile> {
    return request(ADMIN_PROFILE_ENDPOINT, adminProfileSchema);
  },

  async registerAdmin(payload: AdminRegisterPayload): Promise<AdminRegisterResponse> {
    adminRegisterPayloadSchema.parse(payload);
    return post(ADMIN_PROFILE_ENDPOINT, adminRegisterResponseSchema, {
      body: payload,
    });
  },

  async changePassword(adminId: string | number, payload: AdminPasswordChangePayload): Promise<AdminPasswordChangeResponse> {
    adminPasswordChangePayloadSchema.parse(payload);
    return put(`${ADMIN_PROFILE_ENDPOINT}/${adminId}/password`, adminPasswordChangeResponseSchema, {
      body: payload,
    });
  },

  async deleteAdmin(adminId: string | number): Promise<AdminDeleteResponse> {
    return del(`${ADMIN_PROFILE_ENDPOINT}/${adminId}`, adminDeleteResponseSchema);
  },

  async listServiceCategories(): Promise<AdminServiceCategoryList> {
    return request(ADMIN_SERVICE_CATEGORIES_ENDPOINT, adminServiceCategoryListSchema);
  },

  async createServiceCategory(payload: AdminServiceCategoryCreatePayload): Promise<AdminServiceCategoryCreateResponse> {
    const normalized = adminServiceCategoryCreatePayloadSchema.parse(payload);
    return post(ADMIN_SERVICE_CATEGORIES_ENDPOINT, adminServiceCategoryCreateResponseSchema, {
      body: normalized,
    });
  },

  async updateServiceCategory(
    identifier: string | number,
    payload: AdminServiceCategoryUpdatePayload,
  ): Promise<AdminServiceCategoryUpdateResponse> {
    const normalized = adminServiceCategoryUpdatePayloadSchema.parse(payload);
    return put(`${ADMIN_SERVICE_CATEGORIES_ENDPOINT}/${identifier}`, adminServiceCategoryUpdateResponseSchema, {
      body: normalized,
    });
  },

  async listAdminServices(params?: ListServicesParams) {
    return request(ADMIN_SERVICES_ENDPOINT, servicePageSchema, {
      query: buildServicesQuery(params),
    });
  },

  async createService(payload: AdminServiceCreatePayload): Promise<AdminServiceCreateResponse> {
    const normalized = adminServiceCreatePayloadSchema.parse(payload);
    return post(ADMIN_SERVICES_ENDPOINT, adminServiceCreateResponseSchema, {
      body: normalized,
    });
  },

  async updateService(identifier: string | number, payload: AdminServiceUpdatePayload): Promise<AdminServiceUpdateResponse> {
    const normalized = adminServiceUpdatePayloadSchema.parse(payload);
    return put(`${ADMIN_SERVICES_ENDPOINT}/${identifier}`, adminServiceUpdateResponseSchema, {
      body: normalized,
    });
  },

  async getAdminService(identifier: string | number): Promise<ServiceItem> {
    return request(`${ADMIN_SERVICES_ENDPOINT}/${identifier}`, serviceItemSchema);
  },

  async listNews(params?: ListNewsParams): Promise<NewsPageResponse> {
    return request(ADMIN_NEWS_ENDPOINT, newsPageSchema, {
      query: buildNewsQuery(params),
    });
  },

  async createNews(payload: AdminNewsCreatePayload): Promise<AdminNewsCreateResponse> {
    adminNewsCreatePayloadSchema.parse(payload);
    return post(ADMIN_NEWS_ENDPOINT, adminNewsCreateResponseSchema, {
      body: payload,
    });
  },

  async updateNews(identifier: string, payload: AdminNewsUpdatePayload): Promise<AdminNewsUpdateResponse> {
    adminNewsUpdatePayloadSchema.parse(payload);
    return put(`${ADMIN_NEWS_ENDPOINT}/${identifier}`, adminNewsUpdateResponseSchema, {
      body: payload,
    });
  },

  logout() {
    setAdminAuthToken(null);
  },
};
