// File Path: monorepo/web/frontend/src/pages/Admin/Facilitator/Search/CriteriaPage.jsx

import React, { useState, useEffect } from "react";
import { useNavigate } from "react-router";
import { useAuthManager } from "../../../../services/Services";
import {
  MagnifyingGlassIcon,
  WrenchScrewdriverIcon,
  EnvelopeIcon,
  PhoneIcon,
  BuildingOffice2Icon,
  UserIcon,
  HomeIcon,
} from "@heroicons/react/24/outline";
import {
  SearchCriteriaPageComponent,
} from "../../../../components/UIX";

function AdminFacilitatorSearchCriteriaPage() {
  const authManager = useAuthManager();
  const navigate = useNavigate();

  // Form states
  const [formData, setFormData] = useState({
    firstName: "",
    lastName: "",
    email: "",
    phone: "",
    organizationName: "",
    tags: [],
    showOnlyActive: true,
  });
  const [errors, setErrors] = useState({});
  const [isLoading, setIsLoading] = useState(false);

  // Check authentication on mount
  useEffect(() => {
    if (!authManager.isAuthenticated()) {
      navigate("/login");
    }
  }, [authManager, navigate]);

  // Breadcrumb configuration
  const breadcrumbItems = [
    {
      label: "Dashboard",
      to: "/admin/dashboard",
      icon: HomeIcon,
    },
    {
      label: "Facilitators",
      to: "/admin/facilitators",
      icon: WrenchScrewdriverIcon,
    },
    {
      label: "Advanced Search",
      isActive: true,
    },
  ];

  // Form sections configuration
  const formSections = [
    {
      title: "Personal Information",
      fields: [
        {
          name: "firstName",
          label: "First Name",
          type: "text",
          placeholder: "Enter first name",
          icon: UserIcon,
        },
        {
          name: "lastName",
          label: "Last Name",
          type: "text",
          placeholder: "Enter last name",
          icon: UserIcon,
        },
      ],
    },
    {
      title: "Contact Information",
      fields: [
        {
          name: "email",
          label: "Email Address",
          type: "email",
          placeholder: "Enter email address",
          icon: EnvelopeIcon,
        },
        {
          name: "phone",
          label: "Phone Number",
          type: "tel",
          placeholder: "Enter phone number",
          icon: PhoneIcon,
        },
      ],
    },
    {
      title: "Organization",
      fields: [
        {
          name: "organizationName",
          label: "Organization Name",
          type: "text",
          placeholder: "Enter organization name (for commercial facilitators)",
          icon: BuildingOffice2Icon,
        },
      ],
    },
    {
      title: "Tags",
      columns: 1,
      fields: [
        {
          name: "tags",
          label: "",
          fieldType: "tags",
          placeholder: "Select tags to filter facilitators...",
          helperText: "Select one or more tags to filter facilitators",
        },
      ],
    },
  ];

  // Checkbox options
  const checkboxOptions = [
    {
      name: "showOnlyActive",
      label: "Show only active facilitators",
    },
  ];

  // Handle form submission
  const handleSubmit = (data) => {
    setIsLoading(true);

    // Build query parameters
    const queryParams = new URLSearchParams();
    if (data.firstName) queryParams.append("fn", data.firstName);
    if (data.lastName) queryParams.append("ln", data.lastName);
    if (data.email) queryParams.append("e", data.email);
    if (data.phone) queryParams.append("p", data.phone);
    if (data.organizationName) queryParams.append("on", data.organizationName);
    if (data.tags && data.tags.length > 0) queryParams.append("tags", data.tags.join(","));
    if (data.showOnlyActive) queryParams.append("active", "1");

    // Navigate to results page
    const queryString = queryParams.toString();
    navigate(`/admin/facilitators/search-result${queryString ? `?${queryString}` : ""}`);
  };

  // Handle cancel
  const handleCancel = () => {
    navigate("/admin/facilitators");
  };

  // Handle clear form
  const handleClear = () => {
    setFormData({
      firstName: "",
      lastName: "",
      email: "",
      phone: "",
      organizationName: "",
      tags: [],
      showOnlyActive: true,
    });
    setErrors({});
  };

  return (
    <SearchCriteriaPageComponent
      breadcrumbItems={breadcrumbItems}
      pageTitle="Search Facilitators"
      pageIcon={MagnifyingGlassIcon}
      formTitle="Search Criteria"
      formSubtitle="Enter search criteria to find facilitators"
      formIcon={MagnifyingGlassIcon}
      formData={formData}
      onFormDataChange={setFormData}
      formSections={formSections}
      checkboxOptions={checkboxOptions}
      errors={errors}
      onSubmit={handleSubmit}
      onCancel={handleCancel}
      onClear={handleClear}
      submitLabel="Search Facilitators"
      isLoading={isLoading}
      loadingText="Searching..."
    />
  );
}

export default AdminFacilitatorSearchCriteriaPage;
