import { IPlan, PlanFeatures } from "../../Models/Plans/Plans.interface";
import { IPlanSuscriptionDocument } from "./PlanSubscriptions.Adapter.Mongoose";

export type FindPlanSuscripcionsParams = {
    sessionUser: string;
};

export type CreatePlanSuscriptionParams = {
    userId: string;
    planId: string;
    mpPlanId: string;
    startDate: Date;
    endDate: Date;
    autoRenew: boolean;
    mpPayerId?: number;
    mpPayerEmail?: string;
    mpStatus?: string;
    mpDateCreated?: Date;
    mpInitPoint?: string;
    mpPreferenceId?: string;
};

export type CancellPlanSuscriptionParams = {
    sessionUser: string;
};

export type CreateMPPreApprovalParams = {
    planId: string;
    sessionUser: string;
    months: number;
};

export type CheckFeatureParams = {
    userId: string;
    feature: PlanFeatures;
};

export enum MP_PRE_APPROVAL_STATUS {
    READY_TO_START = "ready_to_start",
    NO_PAYER_EMAIL = "no_payer_email",
}

export type MPPreApprovalResponse = {
    init_point: string;
    status: MP_PRE_APPROVAL_STATUS;
};

export type ToFreePlanParams = {
    sessionUser: string;
};

export type ToMpParams = {
    planId: string;
    sessionUser: string;
    months: number;
};

export type CheckMpStatusParams = {
    mpPreferenceId: string;
    sessionUser: string;
};

export type DeletePlanSuscriptionByUserParams = {
    sessionUser: string;
};

export type GetSubscriptionInitPointParams = {
    sessionUser: string;
};

export interface GetSuscriptionInitPointResponse {
    init_point: string;
}

export interface IPlanSuscription {
    id?: string;
    userId: string; // Referencia al ID del usuario
    planId: string; // Referencia al ID del plan suscrito
    mpPlanId: string; //Referencia al ultimo plan contratado por mercadopago.
    startDate: Date; // Fecha de inicio de la suscripción
    endDate: Date; // Fecha de finalización de la suscripción
    isActive: boolean; // Estado de la suscripción (activa o no)
    autoRenew: boolean; // Indica si la suscripción se renueva automáticamente
    paymentMethod: string; // Método de pago usado para la suscripción
    mpPayerId: number; // ID del pagador en MercadoPago.
    mpPayerEmail: string; // Email del pagador en MercadoPago.
    mpStatus: string; // Estado de la suscripción en MercadoPago.
    mpDateCreated: Date; // Fecha de creación de la suscripción en MercadoPago.
    mpInitPoint: string; // URL de inicio de la suscripción en MercadoPago.
    mpPreferenceId: string; // ID de la preferencia en MercadoPago.
}

export interface ISubscriptionInfo {
    id: string;
    plan: IPlan;
    startDate: Date;
    endDate: Date;
    isActive: boolean;
    autoRenew: boolean;
    mpStatus: string;
    mpDateCreated: Date;
}

export interface IPlanSuscriptionsAdapter {
    find(filters: FindPlanSuscripcionsParams): Promise<IPlanSuscription[]>;
    findOne(data: FindPlanSuscripcionsParams): Promise<IPlanSuscriptionDocument | null>;
    create(data: CreatePlanSuscriptionParams): Promise<IPlanSuscriptionDocument>;
    delete(data: CancellPlanSuscriptionParams): Promise<void>;
}

export interface IPlanSuscriptionsManager {
    planSuscriptions: IPlanSuscriptionsAdapter;
    create(data: CreateMPPreApprovalParams): Promise<MPPreApprovalResponse>;
    cancell(data: CancellPlanSuscriptionParams): Promise<void>;
    findOne(data: FindPlanSuscripcionsParams): Promise<IPlanSuscriptionDocument | null>;
    getSubscriptionByUser(data: FindPlanSuscripcionsParams): Promise<ISubscriptionInfo | null>;
    checkFeature(data: CheckFeatureParams): Promise<boolean>;
    isSubscriptionActive(data: FindPlanSuscripcionsParams): Promise<boolean>;
    toFreePlan(data: ToFreePlanParams): Promise<void>;
    toMP(data: ToMpParams): Promise<IPlanSuscriptionDocument>;
    deletePlanSuscriptionByUser(data: DeletePlanSuscriptionByUserParams): Promise<void>;
    getInitPoint(data: GetSubscriptionInitPointParams): Promise<GetSuscriptionInitPointResponse>;
}
