"use client";

import Link from "next/link";
import { useJobs } from "@/lib/hooks/useCareer";

const OpenPosition = () => {
  const { jobs, loading, error } = useJobs();

  if (loading) {
    return (
      <section className="op-position pt-120 pb-120" id="scroll-to">
        <div className="container">
          <div className="row">
            <div className="col-12">
              <div className="intro mb-60">
                <h2 className="mt-8 fw-7 title-anim text-secondary">
                  Open Positions
                </h2>
              </div>
            </div>
            <div className="col-12 mt-60">
              <p className="text-center">Loading positions...</p>
            </div>
          </div>
        </div>
      </section>
    );
  }

  if (error) {
    return (
      <section className="op-position pt-120 pb-120" id="scroll-to">
        <div className="container">
          <div className="row">
            <div className="col-12">
              <div className="intro mb-60">
                <h2 className="mt-8 fw-7 title-anim text-secondary">
                  Open Positions
                </h2>
              </div>
            </div>
            <div className="col-12 mt-60">
              <p className="text-center text-danger">Error loading positions. Please try again later.</p>
            </div>
          </div>
        </div>
      </section>
    );
  }

  if (jobs.length === 0) {
    return (
      <section className="op-position pt-120 pb-120" id="scroll-to">
        <div className="container">
          <div className="row">
            <div className="col-12">
              <div className="intro mb-60">
                <h2 className="mt-8 fw-7 title-anim text-secondary">
                  Open Positions
                </h2>
              </div>
            </div>
            <div className="col-12 mt-60">
              <p className="text-center">No open positions at the moment. Please check back later.</p>
            </div>
          </div>
        </div>
      </section>
    );
  }

  return (
    <section className="op-position pt-120 pb-120" id="scroll-to">
      <div className="container">
        <div className="row">
          <div className="col-12">
            <div className="intro mb-60">
              <h2 className="mt-8 fw-7 title-anim text-secondary">
                Open Positions
              </h2>
            </div>
          </div>
          <div className="col-12 mt-60">
            {jobs.map((job, index) => (
              <div key={job.id} className="op-position-single appear-down">
                <div className="row vertical-column-gap align-items-center">
                  <div className="col-12 col-sm-2">
                    <span className="fw-7 text-xl text-tertiary">
                      {String(index + 1).padStart(2, '0')}
                    </span>
                  </div>
                  <div className="col-12 col-sm-5">
                    <h4 className="fw-7">
                      <Link href={`/career/${job.slug}`}>{job.title}</Link>
                    </h4>
                  </div>
                  <div className="col-12 col-sm-3">
                    <div className="roles">
                      <span className="text-tertiary fw-5 text-xl">
                        ({job.open_positions.toString().padStart(2, '0')} Open {job.open_positions === 1 ? 'Role' : 'Roles'})
                      </span>
                    </div>
                  </div>
                  <div className="col-12 col-sm-2">
                    <div className="cta text-start text-sm-end">
                      <Link href={`/career/${job.slug}`}>
                        <span className="material-symbols-outlined">east</span>
                      </Link>
                    </div>
                  </div>
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
};

export default OpenPosition;
