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, KeenIcon, useDataGrid } from '@/components';
import { toast } from 'sonner';
import { useCallApi } from '@/hooks';
import { Checkbox } from '@/components/ui/checkbox';
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from '@/components/ui/select';
import { doSaveLogActivity } from '@/actions/GlobalActions';

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 EditDialog = () => {
  const parentRef = useRef<any | null>(null);
  const { showEditDialog, handleEditDialog, menus, selectedPosition } = useManagePositionContext();
  const { reload } = useDataGrid();
  const { PutData } = useCallApi();
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [alert, setAlert] = useState({
    show: false,
    message: ''
  });
  const [selectMenus, setSelectMenus] = useState<string[]>([]);
  const [formField, setFormField] = useState({
    name: '',
    status: '',
    status_balance: ''
  });
  const [errors, setErrors] = useState<Record<string, string>>({});
  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;
    }
    doEditPosition(e);
  };
  /* actions */
  const handleCheckboxChange = useCallback((key: string) => {
    setSelectMenus((prev) =>
      prev.includes(key) ? prev.filter((item) => item !== key) : [...prev, key]
    );
  }, []);

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

      if (!selectedPosition) {
        toast.success('Please Select Position');
        return;
      }

      try {
        const response = await PutData(`${API_URL}/user_role/update/${selectedPosition.id}`, {
          name: formField.name,
          roles: selectMenus,
          status: formField.status,
          status_balance: formField.status_balance
        });

        if (response?.status) {
          setAlert((prev) => ({ ...prev, show: false, message: '' }));
          handleEditDialog(false, null);
          toast.success('Success Update Position');
          reload();
          const createActivity = {
            module: 'Manage Position',
            description: `Edit Position => ${selectedPosition.name}`,
            action: 'U'
          };

          doSaveLogActivity(createActivity);
        } else {
          setAlert((prev) => ({ ...prev, show: true, message: response?.message }));
        }
      } catch (error) {
        toast.error('Something went wrong, please try again.');
      } finally {
        setIsSubmitting(false);
      }
    },
    [formField, selectMenus, selectedPosition]
  );

  useEffect(() => {
    if (selectedPosition) {
      setFormField((prev) => ({
        ...prev,
        name: selectedPosition.name,
        status: selectedPosition.status,
        status_balance: selectedPosition.status_balance
      }));

      setSelectMenus(selectedPosition.roles);
    }
  }, [selectedPosition]);

  return (
    <Dialog open={showEditDialog} onOpenChange={(open) => handleEditDialog(open, null)}>
      <DialogContent className="container-fixed max-w-[1024px] flex flex-col p-5 overflow-hidden [&>button]:hidden">
        <DialogHeader className="p-0 border-0">
          <DialogTitle></DialogTitle>
          <DialogDescription></DialogDescription>
          <div className="flex items-center justify-between flex-wrap grow relative">
            <div className="flex flex-col justify-center">
              <h1 className="text-xl font-semibold leading-none text-gray-900">Positions - Edit</h1>
            </div>
            <div
              className="cursor-pointer hover:opacity-100 opacity-50"
              onClick={() => handleEditDialog(false, null)}
            >
              <KeenIcon icon="cross" className="text-1.5xl" />
            </div>
          </div>
          {/* {alert.show && (
            <div className="absolute left-1/2 -translate-x-1/2 top-0 mt-2 z-50 max-w-[20rem]">
              <Alert variant="danger">
                <h3>{alert.message}</h3>
              </Alert>
            </div>
          )} */}
        </DialogHeader>
        <DialogBody className="scrollable-y px-0 pb-0" ref={parentRef}>
          <div className="flex flex-col px-0">
            <form action="" onSubmit={handleSubmit}>
              <div className="card-body grid gap-5 p-0">
                <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</label>
                    <div className="grow flex flex-col">
                      <Input
                        className={errors.name ? 'border-red-500' : ''}
                        type="text"
                        value={formField.name}
                        onChange={({ target }) =>
                          setFormField((prev) => ({ ...prev, name: target.value }))
                        }
                      />
                      {errors.name && <span className="text-red-500 text-sm">{errors.name}</span>}
                    </div>
                  </div>
                </div>

                <div className="w-full">
                  <div className="flex items-center flex-wrap gap-2.5">
                    <label className="form-label max-w-56">Status</label>

                    <div className="grow">
                      <Select
                        value={formField.status}
                        onValueChange={(status) => setFormField((prev) => ({ ...prev, status }))}
                      >
                        <SelectTrigger>
                          <SelectValue placeholder="Select" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="Y">Active</SelectItem>
                          <SelectItem value="N">Non Active</SelectItem>
                        </SelectContent>
                      </Select>
                    </div>
                  </div>
                </div>
                <div className="w-full">
                  <div className="flex items-center flex-wrap gap-2.5">
                    <label className="form-label max-w-56">Status Balance</label>

                    <div className="grow">
                      <Select
                        value={formField.status_balance}
                        onValueChange={(status_balance) => setFormField((prev) => ({ ...prev, status_balance }))}
                      >
                        <SelectTrigger>
                          <SelectValue placeholder="Select" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="Y">Active</SelectItem>
                          <SelectItem value="N">Non Active</SelectItem>
                        </SelectContent>
                      </Select>
                    </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 p-5 pt-2">
                        <MenuItemComponent
                          menu={menu}
                          selectMenus={selectMenus}
                          handleCheckboxChange={handleCheckboxChange}
                        />
                      </div>
                    </div>
                  ))}
                </div>
                <hr />
                <div className="flex justify-end pt-2.5">
                  <Button className="btn btn-primary" type="submit" disabled={isSubmitting}>
                    {isSubmitting ? 'Saving...' : 'Save Changes'}
                  </Button>
                </div>
              </div>
            </form>
          </div>
        </DialogBody>
      </DialogContent>
    </Dialog>
  );
};

export { EditDialog };
