"""Tools for pausing and resuming campaigns.

Provides safety controls to stop outreach when needed
and resume it when ready.
"""

from __future__ import annotations

import json
import logging
import time

from ..db.queries import (
    get_campaign,
    get_setting,
    list_campaigns,
    skip_pending_outreaches,
    update_campaign,
)
from ..db.async_bridge import run_db

logger = logging.getLogger(__name__)


async def run_pause_campaign(campaign_id: str = "") -> str:
    """Pause an active campaign, stopping all outreach.

    If no campaign_id is provided, pauses the first active campaign found.
    """

    setup_done = await run_db(get_setting, "setup_complete", False)
    if not setup_done:
        return (
            "Setup required before managing campaigns.\n\n"
            "Please run setup_profile first."
        )

    # Find the campaign to pause
    if campaign_id:
        campaign = await run_db(get_campaign, campaign_id)
        if not campaign:
            return f"Campaign not found: {campaign_id}"
    else:
        campaigns = await run_db(list_campaigns, status="active")
        if not campaigns:
            return (
                "No active campaigns to pause.\n\n"
                "Use show_status() to see all campaigns."
            )
        campaign = campaigns[0]
        campaign_id = campaign["id"]

    # Check current status
    status = campaign.get("status", "")
    if status == "paused":
        return (
            f"Campaign '{campaign['name']}' is already paused.\n\n"
            "Use resume_campaign() to resume it."
        )
    if status not in ("active", "draft"):
        return (
            f"Campaign '{campaign['name']}' has status '{status}' and cannot be paused.\n"
            "Only active or draft campaigns can be paused."
        )

    # Pause it, skip pending outreaches, and record pause reason
    skipped = await run_db(skip_pending_outreaches, campaign_id)
    cfg = json.loads(campaign.get("config_json") or "{}")
    cfg["pause_reason"] = "user"
    cfg["paused_at"] = int(time.time())
    await run_db(update_campaign, campaign_id, status="paused", config_json=json.dumps(cfg))
    logger.info(f"Paused campaign {campaign_id}: {campaign['name']} ({skipped} pending skipped)")

    return (
        f"Campaign '{campaign['name']}' is now paused.\n\n"
        f"{skipped} pending outreaches marked as skipped.\n"
        "No invitations or messages will be sent.\n\n"
        "Use resume_campaign() when you're ready to continue."
    )


async def run_resume_campaign(campaign_id: str = "") -> str:
    """Resume a paused campaign, re-enabling outreach.

    If no campaign_id is provided, resumes the first paused campaign found.
    """

    setup_done = await run_db(get_setting, "setup_complete", False)
    if not setup_done:
        return (
            "Setup required before managing campaigns.\n\n"
            "Please run setup_profile first."
        )

    # Find the campaign to resume
    if campaign_id:
        campaign = await run_db(get_campaign, campaign_id)
        if not campaign:
            return f"Campaign not found: {campaign_id}"
    else:
        campaigns = await run_db(list_campaigns, status="paused")
        if not campaigns:
            return (
                "No paused campaigns to resume.\n\n"
                "Use show_status() to see all campaigns."
            )
        campaign = campaigns[0]
        campaign_id = campaign["id"]

    # Check current status
    status = campaign.get("status", "")
    if status == "active":
        return (
            f"Campaign '{campaign['name']}' is already active.\n\n"
            "Outreach is running normally."
        )
    if status != "paused":
        return (
            f"Campaign '{campaign['name']}' has status '{status}' and cannot be resumed.\n"
            "Only paused campaigns can be resumed."
        )

    # Resume it and clear pause metadata
    cfg = json.loads(campaign.get("config_json") or "{}")
    cfg.pop("pause_reason", None)
    cfg.pop("paused_at", None)
    await run_db(update_campaign, campaign_id, status="active", config_json=json.dumps(cfg))
    logger.info(f"Resumed campaign {campaign_id}: {campaign['name']}")

    return (
        f"Campaign '{campaign['name']}' is now active again.\n\n"
        "Outreach will resume. Use generate_and_send() to send the next message."
    )
