import { useCallback, useMemo, useState } from "react";
import { parseForm } from "@/shared/forms/validation";
import { useAdminQuery } from "@/shared/hooks/useAdminQuery.js";
import { useToast } from "@/shared/ui/ToastProvider.jsx";
import { createCountry, disableCountry, enableCountry, listCountries, updateCountry } from "@/features/host-org/api";
import { useCountryAbilities } from "@/features/host-org/permissions.js";
import { countryCreateSchema, countryUpdateSchema } from "@/features/host-org/schema";

const emptyCountryForm = () => ({
    countryCode: "",
    countryDisplayName: "",
    countryName: "",
    enabled: true,
    flag: "",
    isoAlpha3: "",
    isoNumeric: "",
    phoneCountryCode: "",
    sortOrder: 0,
});
const emptyData = { items: [], total: 0 };

export function useHostCountriesPage() {
    const abilities = useCountryAbilities();
    const { showToast } = useToast();
    const [query, setQuery] = useState("");
    const [enabledStatus, setEnabledStatus] = useState("");
    const [countryForm, setCountryForm] = useState(emptyCountryForm);
    const [editingCountry, setEditingCountry] = useState(null);
    const [loadingAction, setLoadingAction] = useState("");
    const [activeAction, setActiveAction] = useState("");
    const filters = useMemo(
        () => ({
            enabled: enabledStatus === "enabled" ? true : enabledStatus === "disabled" ? false : undefined,
        }),
        [enabledStatus],
    );
    const queryFn = useCallback(() => listCountries(filters), [filters]);
    const {
        data = emptyData,
        error,
        loading,
        reload,
    } = useAdminQuery(queryFn, {
        errorMessage: "加载国家列表失败",
        initialData: emptyData,
        keepPreviousData: true,
        queryKey: ["host-org", "countries", filters],
    });
    const items = useMemo(() => filterCountries(data?.items || [], query), [data?.items, query]);

    const changeQuery = (value) => {
        setQuery(value);
    };

    const changeEnabledStatus = (value) => {
        setEnabledStatus(value);
    };

    const resetFilters = () => {
        setQuery("");
        setEnabledStatus("");
    };

    const openCreateCountry = () => {
        setEditingCountry(null);
        setCountryForm(emptyCountryForm());
        setActiveAction("create");
    };

    const openEditCountry = (country) => {
        setEditingCountry(country);
        setCountryForm({
            countryCode: country.countryCode || "",
            countryDisplayName: country.countryDisplayName || "",
            countryName: country.countryName || "",
            enabled: Boolean(country.enabled),
            flag: country.flag || "",
            isoAlpha3: country.isoAlpha3 || "",
            isoNumeric: country.isoNumeric || "",
            phoneCountryCode: country.phoneCountryCode || "",
            sortOrder: country.sortOrder || 0,
        });
        setActiveAction("edit");
    };

    const submitCountry = async (event) => {
        event.preventDefault();
        const isEdit = activeAction === "edit";
        await runAction(isEdit ? "country-edit" : "country-create", isEdit ? "国家已更新" : "国家已创建", async () => {
            const payload = parseForm(isEdit ? countryUpdateSchema : countryCreateSchema, countryForm);
            const data = isEdit ? await updateCountry(editingCountry.countryId, payload) : await createCountry(payload);
            setCountryForm(emptyCountryForm());
            setEditingCountry(null);
            setActiveAction("");
            await reload();
            return data;
        });
    };

    const toggleCountry = async (country, nextEnabled = !country.enabled) => {
        if (!abilities.canStatus || Boolean(country.enabled) === nextEnabled) {
            return;
        }
        await runAction(`country-status-${country.countryId}`, nextEnabled ? "国家已启用" : "国家已禁用", async () => {
            const data = nextEnabled ? await enableCountry(country.countryId) : await disableCountry(country.countryId);
            await reload();
            return data;
        });
    };

    const runAction = async (action, successMessage, submitter) => {
        setLoadingAction(action);
        try {
            await submitter();
            showToast(successMessage, "success");
        } catch (err) {
            showToast(err.message || "操作失败", "error");
        } finally {
            setLoadingAction("");
        }
    };

    return {
        abilities,
        activeAction,
        changeEnabledStatus,
        changeQuery,
        closeAction: () => setActiveAction(""),
        countryForm,
        data: { ...data, items, total: items.length },
        editingCountry,
        enabledStatus,
        error,
        loading,
        loadingAction,
        openCreateCountry,
        openEditCountry,
        query,
        reload,
        resetFilters,
        setCountryForm,
        submitCountry,
        toggleCountry,
    };
}

function filterCountries(items, query) {
    const keyword = query.trim().toLowerCase();
    if (!keyword) {
        return items;
    }
    return items.filter((item) => {
        return [
            item.countryCode,
            item.countryDisplayName,
            item.countryName,
            item.isoAlpha3,
            item.isoNumeric,
            item.phoneCountryCode,
        ]
            .filter(Boolean)
            .some((value) => String(value).toLowerCase().includes(keyword));
    });
}
