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

import React, { useState, useEffect } from "react";
import { useNavigate } from "react-router";
import { useAuthManager } from "../../../../services/Services";
import {
  MagnifyingGlassIcon,
  CalendarIcon,
  BuildingOfficeIcon,
  UserGroupIcon,
  HomeIcon,
  DocumentTextIcon,
} from "@heroicons/react/24/outline";
import {
  SearchCriteriaPageComponent,
} from "../../../../components/UIX";

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

  // Form states
  const [formData, setFormData] = useState({
    name: "",
    organization: "",
    customerName: "",
    startDateFrom: "",
    startDateTo: "",
    status: "",
    type: "",
    sector: "",
    location: "",
    tags: [],
    showOnlyActive: true,
  });
  const [errors, setErrors] = useState({});
  const [isLoading, setIsLoading] = useState(false);

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

  // Breadcrumb configuration
  const breadcrumbItems = [
    {
      label: "Dashboard",
      to: "/admin/dashboard",
      icon: HomeIcon,
    },
    {
      label: "Event Contracts",
      to: "/admin/event-contracts",
      icon: DocumentTextIcon,
    },
    {
      label: "Advanced Search",
      isActive: true,
    },
  ];

  // Form sections configuration
  const formSections = [
    {
      fields: [
        {
          name: "name",
          label: "Event Contract Name",
          type: "text",
          placeholder: "Enter event contract name",
          icon: DocumentTextIcon,
        },
        {
          name: "organization",
          label: "Organization",
          type: "text",
          placeholder: "Enter organization name",
          icon: BuildingOfficeIcon,
        },
      ],
    },
    {
      fields: [
        {
          name: "customerName",
          label: "Customer Name",
          type: "text",
          placeholder: "Enter customer name",
          icon: UserGroupIcon,
        },
        {
          name: "location",
          label: "Location",
          type: "text",
          placeholder: "Enter location/address",
          icon: HomeIcon,
        },
      ],
    },
    {
      columns: 2,
      fields: [
        {
          name: "startDateFrom",
          label: "Start Date From",
          type: "date",
          icon: CalendarIcon,
        },
        {
          name: "startDateTo",
          label: "Start Date To",
          type: "date",
          icon: CalendarIcon,
        },
      ],
    },
    {
      title: "Tags",
      columns: 1,
      fields: [
        {
          name: "tags",
          label: "",
          fieldType: "tags",
          placeholder: "Select tags to filter...",
          helperText: "Select one or more tags to filter results",
        },
      ],
    },
  ];


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

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

    // Build query parameters
    const queryParams = new URLSearchParams();
    if (data.name) queryParams.append("name", data.name);
    if (data.organization) queryParams.append("org", data.organization);
    if (data.customerName) queryParams.append("customer", data.customerName);
    if (data.startDateFrom) queryParams.append("start_from", data.startDateFrom);
    if (data.startDateTo) queryParams.append("start_to", data.startDateTo);
    if (data.location) queryParams.append("location", data.location);
    if (data.tags && data.tags.length > 0) queryParams.append("tags", data.tags.join(","));
    if (data.showOnlyActive) queryParams.append("active_only", "true");

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

  // Handle cancel
  const handleCancel = () => {
    navigate("/admin/event-contracts");
  };

  // Handle clear form
  const handleClear = () => {
    setFormData({
      name: "",
      organization: "",
      customerName: "",
      startDateFrom: "",
      startDateTo: "",
      status: "",
      type: "",
      sector: "",
      location: "",
      tags: [],
      showOnlyActive: true,
    });
    setErrors({});
  };

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

export default AdminEventContractSearchCriteriaPage;