import type { Flair } from './FlairStore.svelte';
import { post } from './utils';

export type AsEnum<T> = T[keyof T];

export const ApplicationStatus = Object.freeze({
    ToApply: 0,
    WorkingOnIt: 1,
    Ignore: 2,
    ApplyedButSaidNo: 3,
    Applyed: 4,
    Expired: 5,
    TasksToDo: 6,
    LinkedApplication: 7,
    InterviewStep1: 8
});

export const ApplicationStatusIconMaping: Record<AsEnum<typeof ApplicationStatus>, string> = Object.freeze({
    0: 'clock',
    1: 'search',
    2: 'trash3',
    3: 'fire',
    4: 'send',
    5: 'hourglass-bottom',
    6: 'list-check',
    7: 'link-45deg',
    8: 'person'
});

export const ApplicationStatusMaping: Record<AsEnum<typeof ApplicationStatus>, string> = Object.freeze({
    0: 'To Apply',
    1: 'Working On It',
    2: 'Ignore',
    3: 'Applyed But Said No',
    4: 'Applyed',
    5: 'Expired',
    6: 'Tasks To Do',
    7: 'Linked Application',
    8: 'Interview 1'
});

export type View = {
    id: string;
    application_id: string;
    time: string;
};

export const EventType = Object.freeze({
    Creation: 0,
    StatusUpdate: 1,
    View: 2,
});

export type ApplicationEvent = {
    id: string,
    application_id: string,
    event_type: AsEnum<typeof EventType>,
    new_status: number,
    time: string
}

export type Application = {
    id: string;
    url: string;
    original_url: string | null;
    unique_url: string | null;
    title: string;
    user_id: string;
    extra_data: string;
    payrange: string;
    status: AsEnum<typeof ApplicationStatus>;
    recruiter: string;
    company: string;
    message: string;
    linked_application: string;
    application_time: string;
    create_time: string;
    status_history: string;
    flairs: Flair[];
    views: View[];
    events: ApplicationEvent[];
};

function createApplicationStore() {
    let applications: Application[] = $state([]);
    let applyed: Application[] = $state([]);
    let all: Application[] = $state([]);

    let dragApplication: Application | undefined = $state(undefined);

    let loadItem: Application | undefined = $state(undefined);

    return {
        /**
         * @throws {Error}
         */
        async loadApplications(force = false) {
            if (!force && applications.length > 1) {
                return;
            }
            applications = await post('application/list', { status: 0 });
        },

        /**
         * @throws {Error}
         */
        async loadAplyed(force = false) {
            if (!force && applyed.length > 1) {
                return;
            }
            applyed = await post('application/list', { status: ApplicationStatus.Applyed, views: true });
        },

        /**
         * @throws {Error}
         */
        async loadAll(force = false) {
            if (!force && all.length > 1) {
                return;
            }
            all = await post('application/list', {});
        },

        clear() {
            applications = [];
        },

        dragStart(application: Application | undefined) {
            if (!application) {
                return;
            }
            dragApplication = application;
        },

        dragEnd() {
            dragApplication = undefined;
        },

        get dragging() {
            return dragApplication;
        },

        get applications() {
            return applications;
        },

        get applyed() {
            return applyed;
        },

        get all() {
            return all;
        },

        get loadItem() {
            return loadItem;
        },

        set loadItem(item: Application | undefined) {
            loadItem = item;
        }
    };
}

export const applicationStore = createApplicationStore();
