"""Naukri job-board provider.

Naukri listings require salary (ctc_band) and experience (experience_band) —
JDs missing those fail fast with a clear error recorded on the posting row.
Transport is simulated until Naukri RMS credentials are configured.
"""

import logging
import uuid

from .base import JobBoardIntegration, PostResult

logger = logging.getLogger(__name__)


class NaukriIntegration(JobBoardIntegration):
    channel = "NAUKRI"

    REQUIRED_FIELDS = ("title", "location", "work_details", "ctc_band", "experience_band")

    def _send(self, method: str, payload: dict) -> dict:
        # Real implementation: Naukri RMS API (https://rms.naukri.com) with
        # client credentials. Simulated while credentials are absent.
        external_id = f"nk-{uuid.uuid4().hex[:12]}"
        return {"id": external_id, "url": f"https://www.naukri.com/job-listings-{external_id}"}

    def post_job(self, job) -> PostResult:
        self.validate(job, self.REQUIRED_FIELDS)
        payload = self.build_payload(job)
        logger.info("Naukri: posting JD %s (%s)", job.id, job.title)
        data = self._send("POST", payload)
        return PostResult(external_post_id=data["id"], external_url=data["url"])

    def update_job(self, job) -> PostResult:
        self.validate(job, self.REQUIRED_FIELDS)
        data = self._send("PUT", self.build_payload(job))
        return PostResult(external_post_id=data["id"], external_url=data["url"])

    def delete_job(self, job) -> None:
        logger.info("Naukri: deleting posting for JD %s", job.id)
        self._send("DELETE", {"job_id": job.id})
