import { useCallback, useEffect, useRef, useState } from 'react';

import {
  Dialog,
  DialogBody,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle
} from '@/components/ui/dialog';
import { useManagePositionContext } from '../hooks';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { apiConfig } from '@/config/api.config';
import { Alert, useDataGrid } from '@/components';
import { toast } from 'sonner';
import { useCallApi } from '@/hooks';
import { Checkbox } from '@/components/ui/checkbox';
import { doSaveLogActivity } from '@/actions/GlobalActions';
import { set } from 'date-fns';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';

const API_URL = apiConfig.service_dashboard;

interface MenuItem {
  id: string;
  name: string;
  children?: MenuItem[];
}

const MenuItemComponent: React.FC<{
  menu: MenuItem;
  selectMenus: string[];
  handleCheckboxChange: (id: string) => void;
}> = ({ menu, selectMenus, handleCheckboxChange }) => {
  const { id, name, children = [] } = menu;

  return (
    <div className="mt-3">
      <div className="text-sm flex items-center gap-3">
        <Checkbox
          checked={selectMenus.includes(id)}
          id={`label-${id}`}
          onCheckedChange={() => handleCheckboxChange(id)}
        />
        <label htmlFor={`label-${id}`}>{name}</label>
      </div>
      {children.length > 0 && (
        <div className="pl-5">
          {children.map((child) => (
            <MenuItemComponent
              key={child.id}
              menu={child}
              selectMenus={selectMenus}
              handleCheckboxChange={handleCheckboxChange}
            />
          ))}
        </div>
      )}
    </div>
  );
};

const initialState = {
  name: '',
  status_balance: '',
};

const AddDialog = () => {
  const parentRef = useRef<any | null>(null);
  const { showAddDialog, handleAddDialog, menus } = useManagePositionContext();
  const { reload } = useDataGrid();
  const { PostData } = useCallApi();
  const [alert, setAlert] = useState({
    show: false,
    message: ''
  });
  const [selectMenus, setSelectMenus] = useState<string[]>([]);
  const [formField, setFormField] = useState(initialState);
  const [errors, setErrors] = useState<Record<string, string>>({});
  /* actions */
  const handleCheckboxChange = useCallback((key: string) => {
    setSelectMenus((prev) =>
      prev.includes(key) ? prev.filter((item) => item !== key) : [...prev, key]
    );
  }, []);

  const [isSubmitting, setIsSubmitting] = useState(false);

  const resetForm = () => {
    setFormField(initialState);
    setErrors(() => ({}));
    setSelectMenus([]);
  };

  const validateForm = () => {
    const requiredFields = [
      { key: 'name', label: 'Position Name' },
      { key: 'status_balance', label: 'Status Balance' }];
    const newErrors: Record<string, string> = {};
    let isValid = true;
    requiredFields.forEach(({ key, label }) => {
      if (
        formField[key as keyof typeof formField] === '' ||
        formField[key as keyof typeof formField] === null ||
        formField[key as keyof typeof formField] === undefined
      ) {
        newErrors[key] = `${label} is required`;
        toast.error(`${label} is required`);
        isValid = false;
      }
    });
    setErrors(newErrors);
    return isValid;
  };

  const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    setIsSubmitting(true);
    if (!validateForm()) {
      setIsSubmitting(false);
      return;
    }

    doCreatePosition(e);
  };

  const doCreatePosition = useCallback(
    async (e: React.FormEvent<HTMLFormElement>) => {
      e.preventDefault();

      const response = await PostData(`${API_URL}/user_role/create`, {
        name: formField.name,
        roles: selectMenus,
        status: 'Y',
        status_balance: formField.status_balance
      });

      if (response?.status) {
        resetForm();
        handleAddDialog(false);
        toast.success('Success Create Position');
        reload();
        const createActivity = {
          module: 'Manage Position',
          description: `Create New Position => ${formField.name}`,
          action: 'C'
        };

        doSaveLogActivity(createActivity);
      } else {
        setAlert((prev) => ({ ...prev, show: true, message: response?.message }));
      }
    },
    [formField, selectMenus]
  );

  useEffect(() => {
    if (showAddDialog === false) {
      resetForm();
    }
  }, [showAddDialog]);

  return (
    <Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
      <DialogContent className="container-fixed max-w-screen-lg flex flex-col p-10 overflow-hidden [&>button]:hidden">
        <DialogHeader className="p-0 border-0">
          <DialogTitle></DialogTitle>
          <DialogDescription></DialogDescription>
          <div className="flex items-center justify-between flex-wrap grow gap-5 pb-7.5">
            <div className="flex flex-col justify-center gap-2">
              <h1 className="text-xl font-semibold leading-none text-gray-900">
                Positions - Create
              </h1>
              <div className="flex items-center gap-2 text-sm font-normal text-gray-700"></div>
            </div>
            <Button
              variant={'outline'}
              color="#ddd"
              size={'sm'}
              onClick={() => handleAddDialog(false)}
            >
              Close
            </Button>
          </div>
        </DialogHeader>
        <DialogBody className="scrollable-y py-0 mb-5 ps-0 pe-3 -me-7" ref={parentRef}>
          <div className="flex flex-col items-stretch grow gap-5 lg:gap-7.5">
            <form action="" onSubmit={handleSubmit}>
              <div className="card-body grid gap-5">
                <div className="w-full">
                  <div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                    <label className="form-label flex items-center gap-1 max-w-56">
                      Name <span className="text-danger">*</span>
                    </label>
                    <div className="grow flex flex-col">
                      <Input
                        className={`input ${errors.name ? 'border-danger' : ''}`}
                        type="text"
                        value={formField.name}
                        onChange={({ target }) =>
                          setFormField((prev) => ({ ...prev, name: target.value }))
                        }
                      />
                      {errors.name && (
                        <span className="text-red-500 text-sm mt-1">{errors.name}</span>
                      )}
                    </div>
                  </div>
                </div>
                <div className="w-full">
                  <div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                    <label className="form-label flex items-center gap-1 max-w-56">
                      Status Balance <span className="text-danger">*</span>
                    </label>
                    <div className="grow flex flex-col">
                     <Select
                        value={formField.status_balance}
                        onValueChange={(value) => {
                          setFormField((prev) => ({ ...prev, status_balance: value }));
                          setErrors((prev) => ({ ...prev, status_balance: '' }));
                        }}
                      >
                        <SelectTrigger className={errors.status ? 'border-red-500' : ''}>
                          <SelectValue placeholder="Select" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="Y">Active</SelectItem>
                          <SelectItem value="N">Inactive</SelectItem>
                        </SelectContent>
                      </Select>
                      {errors.status && (
                        <span className="text-red-500 text-xs mt-1">{errors.status}</span>
                      )}
                    </div>
                  </div>
                </div>

                <div className="grid md:grid-cols-3 w-full gap-5">
                  {menus.map((menu) => (
                    <div className="card" key={menu.id}>
                      <div className="card-body">
                        <MenuItemComponent
                          menu={menu}
                          selectMenus={selectMenus}
                          handleCheckboxChange={handleCheckboxChange}
                        />
                      </div>
                    </div>
                  ))}
                </div>

                <div className="flex justify-end pt-2.5">
                  <Button className="btn btn-primary" type="submit">
                    Save Changes
                  </Button>
                </div>
              </div>
            </form>
          </div>
        </DialogBody>
      </DialogContent>
    </Dialog>
  );
};

export { AddDialog };
