import ApiRequest from "@services/Api.Service";
import { ApiVoidResult } from "@models/Api.VoidResult.type";
import * as Yup from "yup";
import {
    CancellPlanSuscriptionParams,
    GetSubscriptionInitPointParams,
    GetSuscriptionInitPointResponse,
} from "@models/Subscription.Model";
import { ApiError } from "@models/Server.Error.model";
import { AppointmentEventByClient } from "@core/Models/Appointments.model";

export interface GetDashboardSummaryParams {
    sessionUser: string;
}

export interface DashboardMetrics {
    organizationsCount: number;
    employeesCount: number;
    clientsCount: number;
    appointmentsCount: number;
    servicesCount: number;
    repeatsCount: number;
}

export interface DashboardSummaryResult {
    role: "ADMIN" | "EMPLOYEE" | "NONE";
    globalMetrics?: DashboardMetrics;
    personalAgenda: AppointmentEventByClient[];
}

export const getDashboardSummary = async (
    data: GetDashboardSummaryParams
): Promise<DashboardSummaryResult> => {
    return ApiRequest.post<DashboardSummaryResult>("dashboard/summary", data);
};

export interface GetDashboardFinancialsParams {
    sessionUser: string;
    companyId: string;
    timeframe: "week" | "month" | "year";
    employeeId?: string;
}

export interface IncomeDataPoint {
    date: string;
    amount: number;
}

export interface DashboardFinancialsResult {
    currentPeriodTotal: number;
    previousPeriodTotal: number;
    currentPeriodData: IncomeDataPoint[];
    unpaidToday: number;
}

export const getDashboardFinancials = async (
    data: GetDashboardFinancialsParams
): Promise<DashboardFinancialsResult> => {
    return ApiRequest.post<DashboardFinancialsResult>("dashboard/financials", data);
};

export interface GetCompanyMetricsParams {
    sessionUser: string;
    companyId: string;
}

export interface CompanyMetricsResult {
    subscription: any;
    metrics: DashboardMetrics;
    isOwner: boolean;
}

export const getCompanyMetrics = async (
    data: GetCompanyMetricsParams
): Promise<CompanyMetricsResult> => {
    return ApiRequest.post<CompanyMetricsResult>("dashboard/company-metrics", data);
};

const schemaCancellSubscripcion = Yup.object().shape({
    sessionUser: Yup.string().required("Para crear un turno debe iniciar sesión."),
});

export const cancellSubscription = async (
    data: CancellPlanSuscriptionParams
): Promise<ApiVoidResult> => {
    return new Promise<ApiVoidResult>((resolve, reject) => {
        schemaCancellSubscripcion
            .validate(data, { abortEarly: true })
            .then(() => {
                resolve(ApiRequest.post<ApiVoidResult>("suscriptions/cancell", data));
            })
            .catch((error) => {
                reject(new ApiError(500, error.message));
            });
    });
};

export const getSuscriptionInitPoint = async (
    data: GetSubscriptionInitPointParams
): Promise<GetSuscriptionInitPointResponse> => {
    return ApiRequest.post<GetSuscriptionInitPointResponse>("suscriptions/get-init-point", data);
};
