import { API_BASE_URL } from "@/constants/connection.constant";
import { replaceAllSafe } from "@/lib/helpers/core.helper";
import _ from "lodash";

export const API_CONFIG: Record<string, Record<string, string>> = {
    ['main']: {
        ['base']: API_BASE_URL
    },
    ['auth']: {
        ['otp-send']: '/v1/auth/login',
        ['otp-verify']: '/v1/auth/verify'
    },
    ['home']: {
        ['get']: '/v1/home',
    },
    ['live']: {
        ['get']: '/v1/live',
    },
    ['course']: {
        ['show']: '/v1/course/{id}',
    },
    ['lesson']: {
        ['show']: '/v1/lesson/{id}',
        ['progress']: '/v1/lesson/{id}/progress',
    },
    ['notification']: {
        ['list']: '/v1/notifications',
        ['unread']: '/v1/notifications/unread-count',
        ['read']: '/v1/notifications/{id}/read',
        ['readAll']: '/v1/notifications/read-all',
    },
    // Coach–learner chat (technical-plan 4.11).
    ['chat']: {
        ['list']: '/v1/chats',
        ['start']: '/v1/chats',
        ['unread']: '/v1/chats/unread-count',
        ['show']: '/v1/chats/{id}',
        ['messages']: '/v1/chats/{id}/messages',
        ['message']: '/v1/chats/{id}/messages/{message}',
        ['report']: '/v1/chats/{id}/messages/{message}/report',
        ['read']: '/v1/chats/{id}/read',
    },
    ['gallery']: {
        ['list']: '/v1/gallery',
        ['mine']: '/v1/gallery/mine',
        ['share']: '/v1/gallery',
        ['show']: '/v1/gallery/{id}',
        ['like']: '/v1/gallery/{id}/like',
        ['report']: '/v1/gallery/{id}/report',
    },
    ['practice']: {
        ['list']: '/v1/practice',
        ['store']: '/v1/practice',
        ['streak']: '/v1/streak',
        ['month']: '/v1/streak/month',
    },
    ['referral']: {
        ['visit']: '/v1/referral/{ref}/visit',
    },
    ['user']: {
        ['me']: '/v1/user/get',
        ['update']: "/v1/user/update"
    },
    ['ticket']: {
        ['new']: "/v1/ticket/new",
        ['get']: "/v1/ticket",
        ['detail']: "v1/ticket/{id}",
        ['message']: "/v1/ticket/{id}/message"
    },
    ['catalogue']: {
        ['categories']: "/v1/categories",
        ['courses']: "/v1/courses",
        ['coaches']: "/v1/coaches",
        ['coach']: "/v1/coaches/{slug}",
    },
    ['submission']: {
        ['list']: "/v1/submissions",
        ['show']: "/v1/submissions/{id}",
        ['store']: "/v1/submissions",
    },
    ['order']: {
        ['get']: "/v1/order",
        ['purchase-preview']: "v1/order/purchase/{purchasable_type}/{purchasable_id}",
        ['cancel-pending']: "v1/order/purchase/{purchasable_type}/{purchasable_id}/cancel",
        ['purchase']: "v1/order/purchase",
        ['discount-validate']: "v1/order/discount/validate",
    },
};

export const ApiEndpoint = (category: string, key: string, values: Record<string, any> = {}): string => {
    let result = API_CONFIG[category][key];
    _.map(values, (value, key) => {
        result = replaceAllSafe(result, `{${key}}`, value);
    });
    return result;
}