import HttpClient from "@/utils/services/apiClient";

type IBlog = {
    id: number,
    image_large: string,
    image_medium: string,
    image_thumbnail: string,
    lang_id: number,
    meta_desc: string,
    seo_title: string,
    short_description: string,
    slug: string,
    subtitle: string,
    title: string,
    tag?: string,
    use_view_file: string,
};

type IBlogList = {
    blogs:IBlog[]
    total: number,
    count: number,
    per_page: number,
    currentPage: number,
    totalPages: number,
    lastPage: number,
    previousPageUrl: string,
    nextPageUrl: string,
};

type IBlogDetails = {
    posted_at?:string
    readingTime?:number
    author?:string,

    id: number,
    slug: string,
    title: string,
    subtitle: string,
    meta_desc: string,
    tag: string,
    seo_title: string,
    short_description: string,
    use_view_file: string,
    image_large: string,
    image_medium: string,
    image_thumbnail: string,
    lang_id: number,
    post_body: string
};

class Blog {
    public static async getList(lang: string, page: number = 1, perPage: number = 3): Promise<IBlogList | null> {
        // caching must be  lest then 30 minutes 
        console.log(`/blogs?lang_code=${lang || "fr"}&page=${page}&per_page=${perPage}`)
        const response = await HttpClient.get(`/blogs?lang_code=${lang || "fr"}&page=${page}&per_page=${perPage}`, 0, { 
            baseUrl: process.env.NEXT_PUBLIC_DADYCAR_BACKEND_HOST 
        });
        return response.data;
    }
    public static async search(query: string, lang: string, page: number = 1, perPage: number = 3): Promise<IBlogList | null> {
        //search url shoub be like the commented one
        const response = await HttpClient.get(`/blogs?search=${encodeURIComponent(query)}&lang_code=${lang || "fr"}&page=${page}&per_page=${perPage}`, 0, {
       
        // const response = await HttpClient.get(`/blogs?lang_code=${lang || "fr"}&page=${page}&per_page=${perPage}`, 0, {
            baseUrl: process.env.NEXT_PUBLIC_DADYCAR_BACKEND_HOST
        });
        return response.data;
    }


    public static async getDetails(slug: string,lang:string): Promise<IBlogDetails | null> {
        // caching must be  lest then 30 minutes
        const response = await HttpClient.get(`/blogs/translation/${slug}/${lang || "fr"}`, 0 , {  baseUrl: process.env.NEXT_PUBLIC_DADYCAR_BACKEND_HOST});
        console.log(`/blogs/${slug}`)
        return response.data;
    }
    public static async getAllSlugs(): Promise<IBlogDetails | null> {
        // caching must be  lest then 30 minutes
        const response = await HttpClient.get(`/blogs/slugs/list`, 0 , {  baseUrl: process.env.NEXT_PUBLIC_DADYCAR_BACKEND_HOST});
        console.log(response)
        return response.data;
    }
}
export type { IBlog,IBlogList ,IBlogDetails};
export default Blog;


