'use client';

import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import SectionIcon from '@/components/SectionIcon';
import {
  profileSectionsService,
  ProfileSection,
  CreateSectionDto,
  UpdateSectionDto,
  getErrorMessage,
} from '@/services';

type ModalType = 'create' | 'edit' | 'delete' | null;

export default function ProfileSectionsPage() {
  const router = useRouter();
  const [sections, setSections] = useState<ProfileSection[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState('');
  const [showInactive, setShowInactive] = useState(true);

  // Modal state
  const [modalType, setModalType] = useState<ModalType>(null);
  const [selectedSection, setSelectedSection] = useState<ProfileSection | null>(null);
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [modalError, setModalError] = useState('');

  // Form state
  const [formData, setFormData] = useState<CreateSectionDto>({
    name: '',
    description: '',
    icon: '',
    isActive: true,
    isGlobal: false,
    isRepeatable: false,
    sortOrder: 0,
  });

  useEffect(() => {
    fetchSections();
  }, [showInactive]);

  const fetchSections = async () => {
    setIsLoading(true);
    setError('');
    try {
      const data = await profileSectionsService.getAll(showInactive);
      setSections(data);
    } catch (err) {
      const errorMessage = getErrorMessage(err);
      setError(errorMessage);
      if (errorMessage.includes('Unauthorized')) {
        router.push('/login');
      }
    } finally {
      setIsLoading(false);
    }
  };

  const openCreateModal = () => {
    setFormData({
      name: '',
      description: '',
      icon: '',
      isActive: true,
      isGlobal: false,
      isRepeatable: false,
      sortOrder: 0,
    });
    setModalError('');
    setModalType('create');
  };

  const openEditModal = (section: ProfileSection) => {
    setSelectedSection(section);
    setFormData({
      name: section.name,
      description: section.description || '',
      icon: section.icon || '',
      isActive: section.isActive,
      isGlobal: section.isGlobal,
      isRepeatable: section.isRepeatable || false,
      sortOrder: section.sortOrder,
    });
    setModalError('');
    setModalType('edit');
  };

  const openDeleteModal = (section: ProfileSection) => {
    setSelectedSection(section);
    setModalError('');
    setModalType('delete');
  };

  const closeModal = () => {
    setModalType(null);
    setSelectedSection(null);
    setModalError('');
  };

  const handleCreate = async (e: React.FormEvent) => {
    e.preventDefault();
    setIsSubmitting(true);
    setModalError('');
    try {
      await profileSectionsService.create(formData);
      closeModal();
      fetchSections();
    } catch (err) {
      setModalError(getErrorMessage(err));
    } finally {
      setIsSubmitting(false);
    }
  };

  const handleUpdate = async (e: React.FormEvent) => {
    e.preventDefault();
    if (!selectedSection) return;
    setIsSubmitting(true);
    setModalError('');
    try {
      await profileSectionsService.update(selectedSection.id, formData as UpdateSectionDto);
      closeModal();
      fetchSections();
    } catch (err) {
      setModalError(getErrorMessage(err));
    } finally {
      setIsSubmitting(false);
    }
  };

  const handleDelete = async () => {
    if (!selectedSection) return;
    setIsSubmitting(true);
    setModalError('');
    try {
      await profileSectionsService.delete(selectedSection.id);
      closeModal();
      fetchSections();
    } catch (err) {
      setModalError(getErrorMessage(err));
    } finally {
      setIsSubmitting(false);
    }
  };

  const toggleStatus = async (section: ProfileSection) => {
    try {
      await profileSectionsService.update(section.id, { isActive: !section.isActive });
      fetchSections();
    } catch (err) {
      setError(getErrorMessage(err));
    }
  };

  return (
    <div>
      {/* Page Title */}
      <div className="mb-6">
        <h1 className="text-2xl font-bold text-[#00293d] font-fractul">Profile Sections</h1>
        <p className="text-[#666666] font-serif">Manage profile sections and their fields for agent types</p>
      </div>

      {/* Sections Table */}
      <div className="bg-white rounded-xl shadow-sm border border-[#e5e7eb]">
        <div className="px-6 py-4 border-b border-[#e5e7eb]">
          <div className="flex justify-between items-center">
            <div className="flex items-center space-x-4">
              <label className="flex items-center space-x-2 text-sm text-[#666666] font-serif">
                <input
                  type="checkbox"
                  checked={showInactive}
                  onChange={(e) => setShowInactive(e.target.checked)}
                  className="rounded border-[#e5e7eb] text-[#f5a623] focus:ring-0"
                />
                <span>Show Inactive</span>
              </label>
            </div>
            <div className="flex space-x-3">
              <button
                onClick={fetchSections}
                className="px-4 py-2 border border-[#e5e7eb] hover:bg-[#f5f9f8] text-[#00293d] text-sm font-medium rounded-lg transition-colors flex items-center"
              >
                <svg className="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                </svg>
                Refresh
              </button>
              <button
                onClick={openCreateModal}
                className="px-4 py-2 bg-[#f5a623] hover:bg-[#e09620] text-white text-sm font-medium rounded-lg transition-colors flex items-center"
              >
                <svg className="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
                </svg>
                Add Section
              </button>
            </div>
          </div>
        </div>

        {error && (
          <div className="px-6 py-4 bg-red-50 border-b border-red-200">
            <p className="text-red-700 text-sm">{error}</p>
          </div>
        )}

        <div className="overflow-x-auto">
          {isLoading ? (
            <div className="flex items-center justify-center py-12">
              <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[#f5a623]"></div>
            </div>
          ) : sections.length === 0 ? (
            <div className="text-center py-12">
              <svg className="mx-auto h-12 w-12 text-[#9ca3af]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 10h16M4 14h16M4 18h16" />
              </svg>
              <p className="mt-2 text-[#666666] font-serif">No profile sections found</p>
              <button
                onClick={openCreateModal}
                className="mt-4 px-4 py-2 bg-[#f5a623] hover:bg-[#e09620] text-white text-sm font-medium rounded-lg transition-colors"
              >
                Create First Section
              </button>
            </div>
          ) : (
            <table className="min-w-full divide-y divide-[#e5e7eb]">
              <thead className="bg-[#f5f9f8]">
                <tr>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Section
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Description
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Fields
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Agent Types
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Global
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Status
                  </th>
                  <th className="px-6 py-3 text-right text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Actions
                  </th>
                </tr>
              </thead>
              <tbody className="bg-white divide-y divide-[#e5e7eb]">
                {sections.map((section) => (
                  <tr key={section.id} className="hover:bg-[#f5f9f8]">
                    <td className="px-6 py-4 whitespace-nowrap">
                      <div className="flex items-center">
                        {section.icon && (
                          <span className="mr-3 text-[#666666]">
                            <SectionIcon name={section.icon} className="w-5 h-5" />
                          </span>
                        )}
                        <div>
                          <div className="text-sm font-medium text-[#00293d]">{section.name}</div>
                          <div className="text-xs text-[#666666]">{section.slug}</div>
                        </div>
                      </div>
                    </td>
                    <td className="px-6 py-4">
                      <div className="text-sm text-[#666666] max-w-xs truncate">
                        {section.description || '-'}
                      </div>
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap">
                      <Link
                        href={`/dashboard/profile-sections/${section.id}`}
                        className="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-medium bg-[#e8f0ee] text-[#5ba4a4] hover:bg-[#d5e5e1]"
                      >
                        {section._count?.fields || 0} fields
                        <svg className="ml-1 w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                        </svg>
                      </Link>
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap">
                      <div className="text-sm text-[#666666]">
                        {section.isGlobal ? (
                          <span className="text-[#5ba4a4] font-medium">All (Global)</span>
                        ) : (
                          `${section._count?.agentTypeSections || 0} assigned`
                        )}
                      </div>
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap">
                      <div className="flex items-center space-x-2">
                        {section.isSystem && (
                          <span className="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-[#fff7ed] text-[#f5a623]">
                            System
                          </span>
                        )}
                        <span
                          className={`inline-flex px-2 py-1 text-xs font-semibold rounded-full ${
                            section.isGlobal
                              ? 'bg-[#fff7ed] text-[#f5a623]'
                              : 'bg-[#e8f0ee] text-[#666666]'
                          }`}
                        >
                          {section.isGlobal ? 'Global' : 'Specific'}
                        </span>
                        {section.isRepeatable && (
                          <span className="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-orange-100 text-orange-800">
                            Repeatable
                          </span>
                        )}
                      </div>
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap">
                      <button
                        onClick={() => toggleStatus(section)}
                        className={`inline-flex px-2 py-1 text-xs font-semibold rounded-full cursor-pointer transition-colors ${
                          section.isActive
                            ? 'bg-green-100 text-green-800 hover:bg-green-200'
                            : 'bg-gray-100 text-gray-800 hover:bg-gray-200'
                        }`}
                      >
                        {section.isActive ? 'Active' : 'Inactive'}
                      </button>
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                      <Link
                        href={`/dashboard/profile-sections/${section.id}`}
                        className="text-[#f5a623] hover:text-[#e09620] mr-3 inline-flex items-center gap-1"
                        title="Manage"
                      >
                        <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                        </svg>
                        Manage
                      </Link>
                      <button
                        onClick={() => openEditModal(section)}
                        className="text-[#5ba4a4] hover:text-[#5ba4a4]/80 mr-3 inline-flex items-center gap-1"
                        title="Edit"
                      >
                        <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                        </svg>
                        Edit
                      </button>
                      {section.isSystem ? (
                        <span
                          className="text-[#9ca3af] cursor-not-allowed inline-flex items-center gap-1"
                          title="System sections cannot be deleted"
                        >
                          <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                          </svg>
                          Delete
                        </span>
                      ) : (
                        <button
                          onClick={() => openDeleteModal(section)}
                          className="text-red-600 hover:text-red-900 inline-flex items-center gap-1"
                          title="Delete"
                        >
                          <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                          </svg>
                          Delete
                        </button>
                      )}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          )}
        </div>
      </div>

      {/* Create/Edit Modal */}
      {(modalType === 'create' || modalType === 'edit') && (
        <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
          <div className="bg-white rounded-xl shadow-xl max-w-md w-full mx-4">
            <div className="px-6 py-4 border-b border-[#e5e7eb]">
              <h3 className="text-lg font-semibold text-[#00293d] font-fractul">
                {modalType === 'create' ? 'Create Profile Section' : 'Edit Profile Section'}
              </h3>
            </div>
            <form onSubmit={modalType === 'create' ? handleCreate : handleUpdate}>
              <div className="px-6 py-4 space-y-4">
                {modalError && (
                  <div className="p-3 bg-red-50 border border-red-200 rounded-lg">
                    <p className="text-red-700 text-sm">{modalError}</p>
                  </div>
                )}
                <div>
                  <label className="block text-sm font-medium text-[#00293d] mb-1">
                    Name <span className="text-red-500">*</span>
                  </label>
                  <input
                    type="text"
                    value={formData.name}
                    onChange={(e) => setFormData({ ...formData, name: e.target.value })}
                    className="w-full px-3 py-2 border border-[#e5e7eb] rounded-xl focus:outline-none focus:border-[#f5a623] text-[#00293d] bg-white placeholder:text-[#666666]"
                    placeholder="e.g., Location, Experience, Education"
                    required
                  />
                </div>
                <div>
                  <label className="block text-sm font-medium text-[#00293d] mb-1">
                    Description
                  </label>
                  <textarea
                    value={formData.description}
                    onChange={(e) => setFormData({ ...formData, description: e.target.value })}
                    rows={3}
                    className="w-full px-3 py-2 border border-[#e5e7eb] rounded-xl focus:outline-none focus:border-[#f5a623] text-[#00293d] bg-white placeholder:text-[#666666]"
                    placeholder="Brief description of this section"
                  />
                </div>
                <div>
                  <label className="block text-sm font-medium text-[#00293d] mb-1">
                    Icon (emoji)
                  </label>
                  <input
                    type="text"
                    value={formData.icon}
                    onChange={(e) => setFormData({ ...formData, icon: e.target.value })}
                    className="w-full px-3 py-2 border border-[#e5e7eb] rounded-xl focus:outline-none focus:border-[#f5a623] text-[#00293d] bg-white placeholder:text-[#666666]"
                    placeholder="e.g., location_on"
                  />
                </div>
                <div>
                  <label className="block text-sm font-medium text-[#00293d] mb-1">
                    Sort Order
                  </label>
                  <input
                    type="number"
                    value={formData.sortOrder === 0 ? '' : formData.sortOrder}
                    onChange={(e) => setFormData({ ...formData, sortOrder: parseInt(e.target.value) || 0 })}
                    onFocus={(e) => e.target.select()}
                    placeholder="0"
                    min={0}
                    className="w-full px-3 py-2 border border-[#e5e7eb] rounded-xl focus:outline-none focus:border-[#f5a623] text-[#00293d] bg-white placeholder:text-[#666666]"
                  />
                </div>
                <div className="flex items-center space-x-6">
                  <div className="flex items-center">
                    <input
                      type="checkbox"
                      id="isActive"
                      checked={formData.isActive}
                      onChange={(e) => setFormData({ ...formData, isActive: e.target.checked })}
                      className="rounded border-[#e5e7eb] text-[#f5a623] focus:ring-0"
                    />
                    <label htmlFor="isActive" className="ml-2 text-sm text-[#00293d] font-serif">
                      Active
                    </label>
                  </div>
                  <div className="flex items-center">
                    <input
                      type="checkbox"
                      id="isGlobal"
                      checked={formData.isGlobal}
                      onChange={(e) => setFormData({ ...formData, isGlobal: e.target.checked })}
                      className="rounded border-[#e5e7eb] text-[#5ba4a4] focus:ring-0"
                    />
                    <label htmlFor="isGlobal" className="ml-2 text-sm text-[#00293d] font-serif">
                      Global (all agent types)
                    </label>
                  </div>
                </div>
                <div className="flex items-center">
                  <input
                    type="checkbox"
                    id="isRepeatable"
                    checked={formData.isRepeatable}
                    onChange={(e) => setFormData({ ...formData, isRepeatable: e.target.checked })}
                    className="rounded border-[#e5e7eb] text-[#f5a623] focus:ring-0"
                  />
                  <label htmlFor="isRepeatable" className="ml-2 text-sm text-[#00293d] font-serif">
                    Allow Multiple Entries (e.g., certifications, education)
                  </label>
                </div>
              </div>
              <div className="px-6 py-4 border-t border-[#e5e7eb] flex justify-end space-x-3">
                <button
                  type="button"
                  onClick={closeModal}
                  className="px-4 py-2 border border-[#e5e7eb] text-[#00293d] rounded-xl hover:bg-[#f5f9f8] transition-colors"
                >
                  Cancel
                </button>
                <button
                  type="submit"
                  disabled={isSubmitting}
                  className="px-4 py-2 bg-[#f5a623] text-white rounded-xl hover:bg-[#e09620] transition-colors disabled:opacity-50"
                >
                  {isSubmitting ? 'Saving...' : modalType === 'create' ? 'Create' : 'Save Changes'}
                </button>
              </div>
            </form>
          </div>
        </div>
      )}

      {/* Delete Confirmation Modal */}
      {modalType === 'delete' && selectedSection && (
        <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
          <div className="bg-white rounded-xl shadow-xl max-w-md w-full mx-4">
            <div className="px-6 py-4 border-b border-[#e5e7eb]">
              <h3 className="text-lg font-semibold text-[#00293d] font-fractul">Delete Profile Section</h3>
            </div>
            <div className="px-6 py-4">
              {modalError && (
                <div className="mb-4 p-3 bg-red-50 border border-red-200 rounded-lg">
                  <p className="text-red-700 text-sm">{modalError}</p>
                </div>
              )}
              <p className="text-[#666666] font-serif">
                Are you sure you want to delete <span className="font-semibold">{selectedSection.name}</span>?
              </p>
              {(selectedSection._count?.fields && selectedSection._count.fields > 0) && (
                <p className="mt-2 text-amber-600 text-sm">
                  Warning: This section has {selectedSection._count.fields} field(s). They will also be deleted.
                </p>
              )}
            </div>
            <div className="px-6 py-4 border-t border-[#e5e7eb] flex justify-end space-x-3">
              <button
                onClick={closeModal}
                className="px-4 py-2 border border-[#e5e7eb] text-[#00293d] rounded-xl hover:bg-[#f5f9f8] transition-colors"
              >
                Cancel
              </button>
              <button
                onClick={handleDelete}
                disabled={isSubmitting}
                className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50"
              >
                {isSubmitting ? 'Deleting...' : 'Delete'}
              </button>
            </div>
          </div>
        </div>
      )}
    </div>
  );
}
