import { AxiosResponse } from 'axios';
import { apiClient } from '../lib/apiClient';
import { DeclineSessionData, Session, SessionsFilter, SessionCommentData } from '../types/sessions';

export const getUpcomingSessions = (locale: string, jwt: string, filter?: SessionsFilter): Promise<AxiosResponse<Session[]>> => (
    apiClient.post(
        '/home/upcomingsessionsall',
        {
            sessionType: 'session',
            ...(filter || {})
        },
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);

export const getRequestedSessions = (locale: string, jwt: string): Promise<AxiosResponse<{ requestedSessions: Session[] }>> => (
    apiClient.post(
        '/home/coachhomedata',
        {},
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);

export const getRecentSessions = (locale: string, jwt: string, filter?: SessionsFilter): Promise<AxiosResponse<Session[]>> => (
    apiClient.post(
        '/home/historicalmeetings',
        {
            sessionType: 'session',
            ...(filter || {})
        },
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);

export const getSessionDetails = (locale: string, jwt: string, id: number): Promise<AxiosResponse<Session>> => (
    apiClient.post(
        '/home/session',
        { id },
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);

export const approveRequestedSession = (locale: string, jwt: string, sessionId: number): Promise<AxiosResponse> => (
    apiClient.post(
        '/home/approverequestedsession',
        { sessionId },
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);

export const declineRequestedSession = (locale: string, jwt: string, { sessionId, reason }: DeclineSessionData): Promise<AxiosResponse> => (
    apiClient.post(
        '/home/declinerequestedsession',
        {
            sessionId,
            coachDeclineReason: reason
        },
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);

export const cancelUpcomingSession = (locale: string, jwt: string, { sessionId, reason }: DeclineSessionData): Promise<AxiosResponse> => (
    apiClient.post(
        '/home/cancelupcomingsession',
        {
            sessionId,
            coachCancelReason: reason
        },
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);

export const addSessionComment = (locale: string, jwt: string, data: SessionCommentData): Promise<AxiosResponse> => (
    apiClient.post(
        '/home/session_comment',
        data,
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);

export const trackingStartSession = (locale: string, jwt: string, id: number): Promise<AxiosResponse> => (
    apiClient.post(
        '/home/sessiontracking',
        { id },
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);

export const finishSession = (locale: string, jwt: string, sessionId: number): Promise<AxiosResponse> => (
    apiClient.post(
        '/home/finishsession',
        { sessionId },
        {
            headers: {
                'X-User-Language': locale,
                Authorization: `Bearer ${jwt}`
            }
        }
    )
);
