import type {
    CreateSupportedApp,
    SupportedApp,
    UpdateSupportedApp,
} from "@pkg/logic/domains/supported-app/data";
import {
    createSupportedAppSC,
    deleteSupportedAppSC,
    listSupportedAppsSQ,
    updateSupportedAppSC,
} from "./supported-app.remote";
import { toast } from "svelte-sonner";

class SupportedAppViewModel {
    supportedApps = $state<SupportedApp[]>([]);
    loading = $state(false);
    creating = $state(false);
    updating = $state(false);
    deletingId = $state<number | null>(null);
    showCreateDialog = $state(false);

    async fetchSupportedApps() {
        this.loading = true;
        try {
            const result = await listSupportedAppsSQ();
            if (result?.error || !result?.data) {
                toast.error(
                    result?.error?.message || "Failed to fetch supported apps",
                    {
                        description:
                            result?.error?.description || "Please try again",
                    },
                );
                return;
            }
            this.supportedApps = result.data as SupportedApp[];
        } catch (error) {
            toast.error("Failed to fetch supported apps", {
                description:
                    error instanceof Error
                        ? error.message
                        : "Please try again",
            });
        } finally {
            this.loading = false;
        }
    }

    async createSupportedApp(data: CreateSupportedApp): Promise<boolean> {
        this.creating = true;
        try {
            const result = await createSupportedAppSC(data);
            if (result?.error) {
                toast.error(
                    result.error.message || "Failed to create supported app",
                    {
                        description:
                            result.error.description || "Please try again",
                    },
                );
                return false;
            }
            toast.success("Supported app created");
            this.showCreateDialog = false;
            if (result.data) {
                this.supportedApps = [
                    ...this.supportedApps,
                    result.data as SupportedApp,
                ];
            }
            return true;
        } catch (error) {
            toast.error("Failed to create supported app", {
                description:
                    error instanceof Error
                        ? error.message
                        : "Please try again",
            });
            return false;
        } finally {
            this.creating = false;
        }
    }

    async updateSupportedApp(
        id: number,
        data: UpdateSupportedApp,
    ): Promise<boolean> {
        this.updating = true;
        try {
            const result = await updateSupportedAppSC({ id, data });
            if (result?.error) {
                toast.error(
                    result.error.message || "Failed to update supported app",
                    {
                        description:
                            result.error.description || "Please try again",
                    },
                );
                return false;
            }
            toast.success("Supported app updated");
            if (result.data) {
                const updated = result.data as SupportedApp;
                this.supportedApps = this.supportedApps.map((app) =>
                    app.id === id ? updated : app,
                );
            }
            return true;
        } catch (error) {
            toast.error("Failed to update supported app", {
                description:
                    error instanceof Error
                        ? error.message
                        : "Please try again",
            });
            return false;
        } finally {
            this.updating = false;
        }
    }

    async deleteSupportedApp(id: number) {
        this.deletingId = id;
        try {
            const result = await deleteSupportedAppSC({ id });
            if (result?.error) {
                toast.error(
                    result.error.message || "Failed to delete supported app",
                    {
                        description:
                            result.error.description || "Please try again",
                    },
                );
                return;
            }
            toast.success("Supported app deleted");
            this.supportedApps = this.supportedApps.filter((app) => app.id !== id);
        } catch (error) {
            toast.error("Failed to delete supported app", {
                description:
                    error instanceof Error
                        ? error.message
                        : "Please try again",
            });
        } finally {
            this.deletingId = null;
        }
    }
}

export const supportedAppVM = new SupportedAppViewModel();
