import routes from "@/routes";
import { getAuthHeaders, getBaseHeaders } from "../utils";
import { IUTM } from "@/store/utm";
import { ICreateAuthorizeUser } from "./User";
import { ELocalesPlacement } from "@/locales";
import { Currency } from "@/components/PaymentTable/Price";
import { PeriodType } from "@/hooks/translations";

export interface PayloadCreate {
    feature: string, // Type: string
    locale: string, // Type: string
    timezone: string, // Type: string
    source: string, // Type: string
    sign: boolean, // Type: boolean
    signDate: string | undefined, // Type: string, ISO Date
    utm: IUTM, // Type: { [key: string]: string } - Optional
    domain: string // Type: string
}

export interface PayloadUpdate {
    sessionId: string,
    data: {
        feature: string, // Type: string

        profile?: Partial<ICreateAuthorizeUser>;
        partner?: Partial<Exclude<ICreateAuthorizeUser, "relationship_status">>;
        answers?: Partial<IAnswersSessionPalmistry | IAnswersSessionChats | IAnswersSessionCompatibilityV2 | IAnswersSessionCompatibilityV3 | IAnswersSessionCompatibilityV4>;
        cookies?: Record<string, string>; // Куки браузера
    }
}

export interface IAnswersSessionPalmistry {
    what_aspects: 'love_relationships' | 'health_vitality' | 'career_destiny' | 'life_transitions', // Type: string, optional - 'love_relationships' | 'health_vitality' | 'career_destiny';
    relationship_status: 'single' | 'in_relationship' | 'engaged' | 'divorced' | 'complicated', // Type: string, optional - 'single' | 'in_relationship';
    element_resonates: 'water' | 'fire' | 'air' | 'earth' | 'light' | 'darkness', // Type: string, optional - 'water' | 'fire' | 'air' | 'earth';
    favorite_color: 'blue' | 'green' | 'orange' | 'violet' | 'red' | 'yellow' | 'turquoise', // Type: string, optional - 'blue' | 'green' | 'orange' | 'violet' | 'red' | 'yellow';
    head_or_heart: 'head' | 'heart' | 'both' | 'depends', // Type: string, optional - 'head' | 'heart' | 'both';
    time_alone: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    own_company: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    socializing_in_groups: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    loneliness_avoid: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    activities_independently: 1 | 2 | 3 | 4 | 5 // Type: number, optional - 1 | 2 | 3 | 4 | 5;
}

export interface IAnswersSessionCompatibilityV2 {
    what_aspects: 'love_relationships' | 'health_vitality' | 'career_destiny' | 'life_transitions', // Type: string, optional - 'love_relationships' | 'health_vitality' | 'career_destiny';
    relationship_status: 'single' | 'in_relationship' | 'engaged' | 'divorced' | 'complicated' | 'start' | 'developing' | 'crisis', // Type: string, optional - 'single' | 'in_relationship';
    element_resonates: 'water' | 'fire' | 'air' | 'earth' | 'light' | 'darkness', // Type: string, optional - 'water' | 'fire' | 'air' | 'earth';
    favorite_color: 'blue' | 'green' | 'orange' | 'violet' | 'red' | 'yellow' | 'turquoise', // Type: string, optional - 'blue' | 'green' | 'orange' | 'violet' | 'red' | 'yellow';
    head_or_heart: 'head' | 'heart' | 'both' | 'depends', // Type: string, optional - 'head' | 'heart' | 'both';
    time_alone: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    own_company: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    socializing_in_groups: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    loneliness_avoid: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    activities_independently: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    date_event: string; // Type: string, optional - ISO Date
    partner_expectations: 1 | 2 | 3 | 4 | 5; // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    romantic_gestures: "love" | "neutral" | "dislike"; // Type: string, optional - "love" | "neutral" | "dislike";
    checking_phone: "against" | "allow" | "normally"; // Type: string, optional - "against" | "allow" | "normally";
    fear_in_relationship: "wasting_life_for_wrong_person" | "stay_single" | "skip_destiny" | "love_without_respect_and_support" | "not_find_truth_about_compatibility";
    who_matter: "me" | "kids" | "family";
    your_priority: "inner_harmony" | "reliable_relationship" | "new_impressions";
}

export interface IAnswersSessionCompatibilityV3 {
    what_aspects: 'love_relationships' | 'health_vitality' | 'career_destiny' | 'life_transitions', // Type: string, optional - 'love_relationships' | 'health_vitality' | 'career_destiny';
    relationship_status: 'single' | 'in_relationship' | 'engaged' | 'divorced' | 'complicated', // Type: string, optional - 'single' | 'in_relationship';
    element_resonates: 'water' | 'fire' | 'air' | 'earth' | 'light' | 'darkness', // Type: string, optional - 'water' | 'fire' | 'air' | 'earth';
    favorite_color: 'blue' | 'green' | 'orange' | 'violet' | 'red' | 'yellow' | 'turquoise', // Type: string, optional - 'blue' | 'green' | 'orange' | 'violet' | 'red' | 'yellow';
    head_or_heart: 'head' | 'heart' | 'both' | 'depends', // Type: string, optional - 'head' | 'heart' | 'both';
    time_alone: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    own_company: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    socializing_in_groups: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    loneliness_avoid: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    activities_independently: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    date_event: string; // Type: string, optional - ISO Date
    partner_expectations: 1 | 2 | 3 | 4 | 5; // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    romantic_gestures: "love" | "neutral" | "dislike"; // Type: string, optional - "love" | "neutral" | "dislike";
    checking_phone: "against" | "allow" | "normally"; // Type: string, optional - "against" | "allow" | "normally";
}

export interface IAnswersSessionCompatibilityV4 {
    what_aspects: 'love_relationships' | 'health_vitality' | 'career_destiny' | 'life_transitions', // Type: string, optional - 'love_relationships' | 'health_vitality' | 'career_destiny';
    relationship_status: 'single' | 'start' | 'in_relationship' | 'developing' | 'crisis', // Type: string, optional - 'single' | 'in_relationship';
    result_analysis: 'find_answers' | 'improve_relationships' | 'what_awaits' | 'continue_relationship' | 'just_interested';
    calculate_in_advance: 'yes' | 'no' | 'yes_may_be' | 'yes_but_not_fate', // Type: string, optional - 'single' | 'in_relationship';
    element_resonates: 'water' | 'fire' | 'air' | 'earth' | 'light' | 'darkness', // Type: string, optional - 'water' | 'fire' | 'air' | 'earth';
    favorite_color: 'blue' | 'green' | 'orange' | 'violet' | 'red' | 'yellow' | 'turquoise', // Type: string, optional - 'blue' | 'green' | 'orange' | 'violet' | 'red' | 'yellow';
    head_or_heart: 'head' | 'heart' | 'both' | 'depends', // Type: string, optional - 'head' | 'heart' | 'both';
    time_alone: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    own_company: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    socializing_in_groups: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    loneliness_avoid: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    activities_independently: 1 | 2 | 3 | 4 | 5, // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    date_event: string; // Type: string, optional - ISO Date
    partner_expectations: 1 | 2 | 3 | 4 | 5; // Type: number, optional - 1 | 2 | 3 | 4 | 5;
    romantic_gestures: "love" | "neutral" | "dislike"; // Type: string, optional - "love" | "neutral" | "dislike";
    checking_phone: "against" | "allow" | "normally"; // Type: string, optional - "against" | "allow" | "normally";
    partner_similarity: 'very_similar' | 'average_similar' | 'not_similar';
    complex_relationship_aspect: 'understanding' | 'emotions' | 'views' | 'conflicts';
    stress_response: 'compromise' | 'pause' | 'anger' | 'distance';
    what_add_to_analysis: string // '5_compatibility_zones' | 'analyzing_mistakes_relationships' |
    // 'astrological_triggers' | 'relationship_timing' | 'practices_strengthen_communication' |
    // 'scenarios_times_crisis' | 'astrological_view_conflicts' | 'energy_compatibility'
    partner_password: "normal" | "not_like" | "exchange_passwords";
    your_fear: "loneliness" | "lose_yourself" | "make_mistake";
    your_inclination: "fall_in_love" | "be_careful" | "look_closely";
}

export interface IAnswersSessionChats {
    psychicReading: string;
    goal: string;
    relationshipStatus: string;
    satisfiedWithLife: string;
    parentStatus: string;
    whatEmotion: string;
    whatYouWant: string;
    missingInLife: string;
    yourGoal: string;
    somethingWorrying: string;
    proneToOverthinking: string;
    worriesImpact: string;
    sensitiveToCriticism: string;
    headOrHeart: string;
    howConfident: string;
    needGuidance: string;
    decisionGuidance: string;
    resonateLove: string;
    resonateLife: string;
    resonateFuture: string;
    believeInSpirituality: string;
    readingExperience: string;
    interestedHowLong: string;
    mainReason: string;
    whatToExpect: string;
    guidanceArea: string[];
    psychicComfortable: string[];
    userName: string;
}

export interface ResponseCreate {
    status: "success" | string,
    sessionId: string
}

export interface ResponseUpdate {
    status: "success" | string,
    message: "Session updated" | string
}

export interface ResponseGetLocale {
    male: Record<string, string>
    female: Record<string, string>
    fallback: {
        male: Record<string, string>,
        female: Record<string, string>
    }
}

export interface ResponseGetPixels {
    status: "success" | string,
    data: {
        fb?: string[];
    }
}

export interface ResponseGetFunnel {
    status: "success" | string;
    data: IFunnel;
}

export interface IFunnel {
    currency: Currency,
    funnel: ELocalesPlacement,
    locale: string,
    payment: Record<string, IFunnelPaymentPlacement | null>
}

export interface IFunnelPaymentProperty {
    key: string,
    value: string | number
}

export interface IFunnelPaymentVariant {
    id: string,
    key: string,
    type: string,
    price: number,
    oldPrice?: number,
    trialPrice?: number
}

export interface IFunnelPaymentPlacement {
    price?: number,
    currency?: Currency,
    billingPeriod?: PeriodType,
    billingInterval?: number,
    trialPeriod?: PeriodType,
    trialInterval?: number,
    placementId?: string,
    paywallId?: string,
    properties?: IFunnelPaymentProperty[],
    variants?: IFunnelPaymentVariant[],
    paymentUrl?: string
}

export const createRequest = (data: PayloadCreate) => {
    const url = new URL(routes.server.createSession());
    const body = JSON.stringify(data);
    return new Request(url, {
        method: "POST",
        body,
        headers: getBaseHeaders()
    });
};

export const updateRequest = ({ data, sessionId }: PayloadUpdate) => {
    const url = new URL(routes.server.updateSession(sessionId));
    const body = JSON.stringify(data);
    return new Request(url, {
        method: "PATCH",
        body,
        headers: getBaseHeaders()
    });
};

export interface PayloadGetLocale {
    funnel: ELocalesPlacement,
    locale: string
}

export const getLocaleRequest = (data: PayloadGetLocale) => {
    const url = new URL(routes.server.getLocaleCDN());
    
    // Добавляем параметры запроса в URL
    const params = new URLSearchParams();
    params.append('funnel', data.funnel);
    params.append('locale', data.locale);
    url.search = params.toString();
    
    return new Request(url, {
        method: "GET",
        headers: getBaseHeaders()
    });
};

export interface PayloadGetPixels {
    domain: string;
    source: string;
    locale: string;
}

export interface PayloadGetFunnel {
    funnel: ELocalesPlacement;
    token: string;
}

export const getPixelsRequest = ({ domain, source, locale }: PayloadGetPixels) => {
    const url = new URL(routes.server.getPixels());
    const query = new URLSearchParams({
        domain,
        source,
        locale
    });
    url.search = query.toString();
    return new Request(url, {
        method: "GET",
        headers: getBaseHeaders()
    });
};

export const getFunnelRequest = ({ funnel, token }: PayloadGetFunnel) => {
    const url = new URL(routes.server.getFunnel());

    const body = JSON.stringify({ funnel });
    return new Request(url, {
        method: "POST",
        body,
        headers: getAuthHeaders(token)
    });
};