import { create } from "zustand";
import { immer } from "zustand/middleware/immer";

interface VersionStore {
    versionHistory: VersionHistory;
    selectedVersion: Version | null;
    viewVersionHistory: boolean;
    createNewVersion: boolean;

    setSelectedVersion: (version: Version) => void;
    clearSelectedVersion: () => void;

    setVersionHistoryVisible: (visibility: boolean) => void;
    setCreateNewVersion: (createNewVersion: boolean) => void;

    addVersion: (version: Version) => void;
    setVersions: (versions: Version[]) => void;
    clearVersions: () => void;

    setVersionName: (versionId: string, versionName: string) => void;
    updateVersion: (versionId: string, versionName: string, versionDescription: string) => void;

    getVersionById: (versionId: string) => Version | undefined;
}

export const createVersionStore = () => {
    return create<VersionStore>()(
        immer((set, get) => ({
            versionHistory: [],
            selectedVersion: null,
            viewVersionHistory: false,
            createNewVersion: false,

            setSelectedVersion: (version) => {
                set((state) => {
                    state.selectedVersion = version;
                });
            },

            clearSelectedVersion: () => {
                set((state) => {
                    state.selectedVersion = null;
                });
            },

            setVersionHistoryVisible: (visibility: boolean) => {
                set((state) => {
                    state.viewVersionHistory = visibility;
                });
            },

            setCreateNewVersion: (createNewVersion: boolean) => {
                set((state) => {
                    state.createNewVersion = createNewVersion;
                });
            },

            addVersion: (version: Version) => {
                set((state) => {
                    state.versionHistory.unshift(version);
                });
            },

            setVersions: (versions: Version[]) => {
                set((state) => {
                    state.versionHistory = versions;
                });
            },

            clearVersions: () => {
                set((state) => {
                    state.versionHistory = [];
                    state.selectedVersion = null;
                    state.createNewVersion = false;
                });
            },

            setVersionName: (versionId: string, versionName: string) => {
                set((state) => {
                    const version = get().getVersionById(versionId);
                    if (version) {
                        version.versionName = versionName;
                    }
                });
            },

            updateVersion: (versionId: string, versionName: string, versionDescription: string) => {
                set((state) => {
                    const version = get().getVersionById(versionId);
                    if (version) {
                        version.versionName = versionName;
                        version.versionDescription = versionDescription;
                    }
                });
            },

            getVersionById: (versionId: string) => {
                return get().versionHistory.find((v) => {
                    return v.versionId === versionId;
                });
            },
        }))
    );
};

export type VersionStoreType = ReturnType<typeof createVersionStore>;
