"""Messaging and Social Share integration provider (WhatsApp, SMS, Telegram)."""

import logging

from apps.jobs.tracking_tokens import build_application_url

from .base import JobBoardIntegration, PostResult

logger = logging.getLogger(__name__)


class SocialShareIntegration(JobBoardIntegration):
    REQUIRED_FIELDS = ("title", "location")

    def __init__(self, channel: str):
        self.channel = channel.upper()

    def post_job(self, job) -> PostResult:
        self.validate(job, self.REQUIRED_FIELDS)
        # The channel travels inside an encrypted token, never as `?source=`.
        url = build_application_url(job, self.channel)
        logger.info("%s: Generating tracking URL for JD %s (%s)", self.channel, job.id, job.title)
        return PostResult(
            external_post_id=f"{self.channel.lower()}-{job.id}",
            external_url=url,
        )

    def update_job(self, job) -> PostResult:
        return self.post_job(job)

    def delete_job(self, job) -> None:
        logger.info("%s: Unpublishing tracking URL for JD %s", self.channel, job.id)


class WhatsAppIntegration(SocialShareIntegration):
    def __init__(self):
        super().__init__("WHATSAPP")


class SMSIntegration(SocialShareIntegration):
    def __init__(self):
        super().__init__("SMS")


class TelegramIntegration(SocialShareIntegration):
    def __init__(self):
        super().__init__("TELEGRAM")
