import { useEffect } from 'react';
import { Link } from 'react-router-dom';
import { PencilIcon } from '@heroicons/react/24/outline';
import { useAuthStore } from '../stores/authStore';
import SettingsLayout from '../components/layout/SettingsLayout';
import ClinicDoctorsManager from '../components/ClinicDoctorsManager';

/**
 * ClinicDoctorsPage — the clinic-owner settings tab for managing the clinic's
 * doctors (list, invite, detach) and pending invitations. Renders inside the
 * settings shell instead of jumping to the standalone admin ClinicDetailPage.
 * Clinic-info editing (name, gallery, addresses, specialties) stays on the full
 * detail page, reachable via the "ویرایش اطلاعات کلینیک" link.
 */
function ClinicDoctorsContent() {
  const { dbUuid, fetchMe } = useAuthStore();

  useEffect(() => {
    if (!dbUuid) fetchMe();
  }, [dbUuid, fetchMe]);

  if (!dbUuid) {
    return (
      <div style={{ padding: 40, textAlign: 'center' }}>
        <p style={{ color: 'var(--text-3)', fontSize: 14 }}>در حال بارگذاری اطلاعات کلینیک...</p>
      </div>
    );
  }

  return (
    <div className="fade-in" style={{ display: 'flex', flexDirection: 'column', gap: 'var(--gap)' }}>
      <div className="card-title-row">
        <div>
          <h1 className="section-title">پزشکان کلینیک</h1>
          <div className="muted">مدیریت پزشکان و دعوت‌نامه‌های کلینیک</div>
        </div>
        <Link className="btn ghost sm" to={`/admin/clinics/${dbUuid}`}>
          <PencilIcon style={{ width: 15, height: 15 }} /> ویرایش اطلاعات کلینیک
        </Link>
      </div>

      <ClinicDoctorsManager clinicUuid={dbUuid} />
    </div>
  );
}

export default function ClinicDoctorsPage() {
  return (
    <SettingsLayout active="clinic-doctors">
      <ClinicDoctorsContent />
    </SettingsLayout>
  );
}
