import { setPageTitle } from '@/store/themeConfigSlice';
import { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import Link from 'next/link';

import { coloredToast } from '@/lib/toast';
import { useRouter } from 'next/router';
import PromotionForm from '@/components/Promotions/Form';
import { createEmptyGroup } from '@/components/Promotions/CategoryRewardForm';
import { SavePromotion } from '@/utils/savePromotion';
import { SaveCategoryReward } from '@/utils/saveCategoryReward';
import { logActivity } from '@/utils/activityLogger';
import Swal from 'sweetalert2';

const AddPromotions = () => {
    const dispatch = useDispatch();
    const router = useRouter();

    useEffect(() => {
        dispatch(setPageTitle('Promotion Add'));
        setIsMounted(true);
    }, [dispatch]);

    const [isMounted, setIsMounted] = useState(false);
    const [isLoading, setIsLoading] = useState(false);

    const [promotion, setPromotion] = useState<any>({
        promotion_type: '',
        promotion_name: '',
        promotion_start: '',
        promotion_end: '',
        promotion_is_active: false,
        promotion_case: [],
        promotion_category: '',
        promotion_include_categories: [],
        promotion_exclude_categories: [],
        promotion_included_products: [],
        promotion_excluded_products: [],
        promotion_unique_users: [],
        promotion_merge_free_items: true,
        promotion_stacking_mode: 'prefer_discount',
        promotion_merch_scope: 'per_order',
        exit_intent_only: false,
        exit_require_login: false,
        promotion_member_only: false,
        promotion_reward_groups: [createEmptyGroup()],
    });

    const isMultiProduct = promotion.promotion_type === 'category_reward';

    const isFormEmpty = () => {
        if (isMultiProduct) {
            return !(promotion.promotion_reward_groups || []).some((g: any) => (g.include_categories || []).length > 0);
        }
        const hasCategories =
            (promotion.promotion_include_categories || []).length > 0 ||
            (promotion.promotion_exclude_categories || []).length > 0 ||
            (promotion.promotion_included_products || []).length > 0 ||
            (promotion.promotion_excluded_products || []).length > 0;

        return !hasCategories;
    };

    const savePromotion = async () => {
        if (isFormEmpty()) {
            const result = await Swal.fire({
                title: 'Form Kosong',
                text: isMultiProduct
                    ? 'Include categories belum diisi. Tetap ingin menyimpan?'
                    : 'Include dan exclude categories/products tidak diisi. Tetap ingin menyimpan?',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Ya, tetap simpan',
                cancelButtonText: 'Batal',
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
            });

            if (!result.isConfirmed) {
                return;
            }
        }

        setIsLoading(true);

        const validationSave = isMultiProduct
            ? SaveCategoryReward(promotion)
            : SavePromotion({
                  promotion_type: promotion.promotion_type,
                  promotion_name: promotion.promotion_name,
                  promotion_start: promotion.promotion_start,
                  promotion_end: promotion.promotion_end,
                  promotion_is_active: promotion.promotion_is_active,
                  promotion_case: promotion.promotion_case,
                  promotion_category: promotion.promotion_category,
                  promotion_include_categories: promotion.promotion_include_categories,
                  promotion_exclude_categories: promotion.promotion_exclude_categories,
                  promotion_included_products: promotion.promotion_included_products,
                  promotion_excluded_products: promotion.promotion_excluded_products,
                  promotion_unique_users: promotion.promotion_unique_users,
                  promotion_merge_free_items: promotion.promotion_merge_free_items ?? true,
                  promotion_member_only: Boolean(promotion.promotion_member_only),
              });

        if (validationSave.status === false) {
            setIsLoading(false);
            return;
        }

        try {
            const api_url = process.env.API_URL;
            const payload = {
                data: validationSave.data,
            };

            const response = await fetch(`${api_url}api/promotions/store`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(payload),
                credentials: 'include',
            });

            if (!response.ok) {
                const res = await response.json();
                coloredToast('warning', res.message);
                setIsLoading(false);
                return;
            }

            coloredToast('success', `promotion created successfully`);
            await logActivity({
                location: 'Promotion',
                description: `Promotion ${promotion.promotion_name} - Tipe ${promotion.promotion_type} Berhasil dibuat`,
            });
            router.push('/promotions');
        } catch (error) {
            console.error(error);
            await logActivity({
                location: 'Promotion',
                description: `Promotion gagal dibuat`,
            });
            coloredToast('danger', 'something went wrong');
        }

        setIsLoading(false);
    };

    return (
        <>
            {isMounted && (
                <>
                    <div className="mb-3">
                        <ul className="flex space-x-2 rtl:space-x-reverse">
                            <li>
                                <Link href="/promotions" className="text-primary hover:underline">
                                    Promotion
                                </Link>
                            </li>
                            <li className="before:content-['/'] ltr:before:mr-2 rtl:before:ml-2">
                                <span>new</span>
                            </li>
                        </ul>
                    </div>
                    <div className="flex flex-col gap-2.5 xl:flex-row">
                        <PromotionForm promotion={promotion} setPromotion={setPromotion} />
                    </div>
                    <div className="mt-6 w-full  xl:w-80">
                        <div className="panel">
                            <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4 xl:grid-cols-1">
                                <button type="button" disabled={isLoading} onClick={savePromotion} className="btn btn-success w-full gap-2">
                                    {isLoading ? (
                                        <span className="inline-block h-5 w-5 animate-spin rounded-full border-2 border-white border-l-transparent align-middle ltr:mr-4 rtl:ml-4"></span>
                                    ) : (
                                        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" className="h-5 w-5 shrink-0 ltr:mr-2 rtl:ml-2">
                                            <path
                                                d="M3.46447 20.5355C4.92893 22 7.28595 22 12 22C16.714 22 19.0711 22 20.5355 20.5355C22 19.0711 22 16.714 22 12C22 11.6585 22 11.4878 21.9848 11.3142C21.9142 10.5049 21.586 9.71257 21.0637 9.09034C20.9516 8.95687 20.828 8.83317 20.5806 8.58578L15.4142 3.41944C15.1668 3.17206 15.0431 3.04835 14.9097 2.93631C14.2874 2.414 13.4951 2.08581 12.6858 2.01515C12.5122 2 12.3415 2 12 2C7.28595 2 4.92893 2 3.46447 3.46447C2 4.92893 2 7.28595 2 12C2 16.714 2 19.0711 3.46447 20.5355Z"
                                                stroke="currentColor"
                                                strokeWidth="1.5"
                                            />
                                            <path
                                                d="M17 22V21C17 19.1144 17 18.1716 16.4142 17.5858C15.8284 17 14.8856 17 13 17H11C9.11438 17 8.17157 17 7.58579 17.5858C7 18.1716 7 19.1144 7 21V22"
                                                stroke="currentColor"
                                                strokeWidth="1.5"
                                            />
                                            <path opacity="0.5" d="M7 8H13" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
                                        </svg>
                                    )}
                                    Save
                                </button>
                            </div>
                        </div>
                    </div>
                </>
            )}
        </>
    );
};
export default AddPromotions;
