import chronicleInstance from "@/shared/services/chronicleInstance";
import { NewsCategoryParams } from "../hooks/useNewsCategory";
import {
  CreateNewsCategoryPayload,
  NewsCategoryItem,
  NewsCategoryResponse,
} from "../types/newsCategoryType";
import { presignedAxios } from "@/shared/services/presignedAxios";

// Helper function to normalize API response data
const normalizeNewsCategoryItem = (item: any): NewsCategoryItem => {
  return {
    ...item,
    // Ensure consistent property names (handle both cases)
    NewsCategoryTitle: item.NewsCategoryTitle ||
      item.newsCategoryTitle || { am: "", en: "" },
    NewsCategoryDescription: item.NewsCategoryDescription ||
      item.newsCategoryDescription || { am: "", en: "" },
  };
};

export const NewsCategoryService = {
  async createItem(data: CreateNewsCategoryPayload): Promise<NewsCategoryItem> {
    const response = await chronicleInstance.post("/news-category", data, {
      headers: { "x-tenant-id": "adf98293-41ba-4bda-bdb4-70e30a70c1b7" },
    });
    return normalizeNewsCategoryItem(response.data);
  },

  async getItems(params: NewsCategoryParams): Promise<NewsCategoryResponse> {
    const response = await chronicleInstance.get(
      `/news-category/list-with-presigned`,
      {
        params,
        headers: { "x-tenant-id": "adf98293-41ba-4bda-bdb4-70e30a70c1b7" },
      }
    );
    return {
      ...response.data,
      items: response.data.items?.map(normalizeNewsCategoryItem) || [],
    };
  },

  async getItem(id: string): Promise<NewsCategoryItem> {
    // Use the list endpoint to get single item with presigned URL
    const response = await chronicleInstance.get(`/news-category/${id}`, {
      headers: { "x-tenant-id": "adf98293-41ba-4bda-bdb4-70e30a70c1b7" },
    });
    return response.data;
  },

  async updateItem(
    id: string,
    data: Partial<CreateNewsCategoryPayload>
  ): Promise<NewsCategoryItem> {
    // Add tenant ID to the payload
    const payload = {
      ...data,
    };
    const response = await chronicleInstance.put(
      `/news-category/${id}`,
      payload,
      { headers: { "x-tenant-id": "adf98293-41ba-4bda-bdb4-70e30a70c1b7" } }
    );
    return normalizeNewsCategoryItem(response.data);
  },

  async deleteItem(id: string): Promise<void> {
    await chronicleInstance.delete(`/news-category/${id}`, {
      headers: { "x-tenant-id": "adf98293-41ba-4bda-bdb4-70e30a70c1b7" },
    });
  },

  async changeStatus(id: string): Promise<NewsCategoryItem> {
    const response = await chronicleInstance.put(
      `/news-category/change-status/${id}`,
      {
        isCurrent: true,
      },
      {
        headers: { "x-tenant-id": "adf98293-41ba-4bda-bdb4-70e30a70c1b7" },
      }
    );
    return normalizeNewsCategoryItem(response.data);
  },

  async uploadImage(file: File, uploadUrl: string) {
    if (!uploadUrl) throw new Error("Upload URL is undefined.");

    return presignedAxios.put(uploadUrl, file, {
      headers: {
        "Content-Type": file.type, // only include this if your backend signed for it
      },
    });
  },

  async updateUploadStatus(
    id: string,
    uploadedSuccessfully: boolean
  ): Promise<void> {
    await chronicleInstance.patch(
      `/news-category/${id}/upload-status`,
      { uploadedSuccessfully },
      {
        headers: { "x-tenant-id": "adf98293-41ba-4bda-bdb4-70e30a70c1b7" },
      }
    );
  },
};
