"use client";

import { useState } from "react";
import useNotification from "@/hooks/useNotifications";
import { Bell, Text, BellRing, Braces } from "lucide-react";

interface AddNotificationProps {
    onNotificationAdded?: () => void;
    onError?: (message: string) => void;
    onSuccess?: (message: string) => void;
}

export default function AddNotification({ onNotificationAdded, onError, onSuccess }: AddNotificationProps) {
    const [name, setName] = useState("");
    const [type, setType] = useState("Select a type");
    const [telegramBotToken, setTelegramBotToken] = useState("");
    const [telegramChatId, setTelegramChatId] = useState("");

    const { addNotification } = useNotification();

    const clearForm = () => {
        setName("")
        setType("Select a type")
        setTelegramBotToken("")
        setTelegramChatId("")
    }

    const addNotificationHandler = async () => {
        const config = type === "TELEGRAM" ? `{ "token": "${telegramBotToken}", "chat_id": "${telegramChatId}" }` : "";
        const response = addNotification(name, type, config);
        if (typeof response === "string") {
            onError && onError(response)
            clearForm();
            return
        }
        try {
            const successMessage = await response
            if (onSuccess && successMessage) {
                onSuccess('Notification Provider added successfully')
                onNotificationAdded && onNotificationAdded()
            }
        } catch (apiError: any) {
            onError && onError(apiError)
        } finally {
            clearForm();
        }
    }

    return (
        <div>
            <dialog id="add_notification" className="modal">
                <div className="modal-box w-11/12 max-w-3xl border-l-4 border-success">
                    <div className="flex items-center gap-3 mb-3">
                        <div className="bg-success text-success-content rounded-full p-2 flex items-center justify-center">
                            <Bell className="h-6 w-6" />
                        </div>
                        <h2 className="text-xl font-bold">Add Notification</h2>
                    </div>

                    <div className="bg-base-200 p-4 rounded-lg mb-4">
                        <div className="space-y-4">
                            <div className="form-control">
                                <div className="flex items-center gap-2">
                                    <label className="label">
                                        <Text className="h-4 w-4 opacity-70" />
                                        <span className="label-text font-medium">Name</span>
                                    </label>
                                </div>
                                <input type="text" className="input input-bordered w-full" placeholder="e.g. Telegram" value={name} onChange={(e) => setName(e.target.value)} />
                            </div>
                            <div className="form-control">
                                <div className="flex items-center gap-2">
                                    <label className="label">
                                        <BellRing className="h-4 w-4 opacity-70" />
                                        <span className="label-text font-medium">Type</span>
                                    </label>
                                </div>
                                <select className="select select-bordered w-full" value={type} onChange={(e) => setType(e.target.value)}>
                                    <option disabled>Select a type</option>
                                    <option value="TELEGRAM">Telegram</option>
                                </select>
                            </div>
                            {type === "TELEGRAM" && (
                                <div className="form-control">
                                    <div className="flex items-center gap-2">
                                        <label className="label">
                                            <Braces className="h-4 w-4 opacity-70" />
                                            <span className="label-text font-medium">Config</span>
                                        </label>
                                    </div>
                                    <div className="flex items-center gap-2">
                                        <input type="text" className="input input-bordered w-full" placeholder="Telegram Bot Token" value={telegramBotToken} onChange={(e) => setTelegramBotToken(e.target.value)} />
                                        <input type="text" className="input input-bordered w-full" placeholder="Telegram Chat ID" value={telegramChatId} onChange={(e) => setTelegramChatId(e.target.value)} />
                                    </div>
                                </div>
                            )}
                        </div>  
                    </div>
                    <div className="modal-action">
                        <form method="dialog" className="flex gap-3 w-full justify-end">
                            <button className="btn btn-outline">Cancel</button>
                            <button className="btn btn-success text-success-content" onClick={addNotificationHandler}>Add</button>
                        </form>
                    </div>
                </div>
            </dialog>
        </div>
    )
}