# retoor <retoor@molodetz.nl>

import logging

from devplacepy.database import add_issue_usage, get_table
from devplacepy.services.gitea import runtime, store
from devplacepy.services.gitea.client import GiteaError
from devplacepy.services.gitea.config import gitea_config
from devplacepy.services.gitea.enhance import enhance_ticket
from devplacepy.services.jobs.base import JobService
from devplacepy.services.openai_gateway.usage import new_usage_totals
from devplacepy.utils import create_notification

logger = logging.getLogger(__name__)


def _attribution(body: str, username: str) -> str:
    return f"{body}\n\n---\n*Reported by **{username}** via DevPlace.*"


class IssueCreateService(JobService):
    kind = "issue_create"
    title = "Issue filing"
    description = (
        "Files submitted issue reports off the request path: rewrites each report into a "
        "consistent, high-quality ticket with the internal AI service, posts it to the "
        "configured Gitea repository, records the local author mapping, and notifies the "
        "reporter. The Gitea issue is permanent; only the job tracking row is swept."
    )

    def __init__(self):
        super().__init__(name="issue_create", interval_seconds=2)

    async def process(self, job: dict) -> dict:
        payload = job["payload"]
        author_uid = payload.get("author_uid", "")
        title = (payload.get("title") or "").strip()
        description = (payload.get("description") or "").strip()

        from devplacepy.services.audit import record as audit

        user = None
        try:
            user = get_table("users").find_one(uid=author_uid)
            if not user:
                raise ValueError(f"reporting user not found: {author_uid}")
            if not title or not description:
                raise ValueError("title and description are required")

            config = gitea_config()
            if not config.is_configured:
                raise GiteaError("Gitea integration is not configured", status=503)

            usage_totals = new_usage_totals()
            enhanced = await enhance_ticket(title, description, config, usage_totals)
            if usage_totals["calls"]:
                add_issue_usage(usage_totals)
            body = _attribution(enhanced.body, user["username"])
            issue = await runtime.get_client().create_issue(enhanced.title, body)

            number = int(issue["number"])
            html_url = issue.get("html_url", "")
            store.record_ticket(
                number=number,
                author_uid=author_uid,
                original_title=title,
                original_description=description,
                enhanced_title=enhanced.title,
                html_url=html_url,
                status=issue.get("state", "open"),
            )

            from devplacepy.services.seo_meta import schedule_seo_meta

            schedule_seo_meta("issue", str(number), regenerate=True)

            attachment_uids = payload.get("attachment_uids") or []
            if attachment_uids:
                from devplacepy.attachments import (
                    link_attachments,
                    mirror_attachment_to_gitea,
                )

                link_attachments(attachment_uids, "issue", str(number))
                for uid in attachment_uids:
                    await mirror_attachment_to_gitea(uid)

            create_notification(
                author_uid,
                "issue",
                f"Your issue report was filed as #{number}: {enhanced.title}",
                str(number),
                f"/issues/{number}",
            )

            audit.record_system(
                "issue.create",
                actor_kind="user",
                actor_uid=author_uid,
                actor_username=user.get("username"),
                origin="devplace",
                target_type="issue",
                target_uid=str(number),
                target_label=enhanced.title,
                metadata={"number": number, "enhanced": enhanced.enhanced, "url": html_url},
                summary=f"{user.get('username')} filed issue #{number}: {enhanced.title}",
                links=[audit.target("issue", str(number), enhanced.title), audit.job(job["uid"])],
            )
            self.log(f"Filed issue #{number} for {user['username']}")
            return {
                "number": number,
                "html_url": html_url,
                "issue_url": f"/issues/{number}",
                "enhanced": enhanced.enhanced,
            }
        except Exception:
            audit.record_system(
                "issue.create",
                actor_kind="user",
                actor_uid=author_uid,
                actor_username=user.get("username") if user else None,
                origin="devplace",
                target_type="issue",
                metadata={"title": title, "description": description[:200]},
                result="failure",
                summary=f"Issue filing failed for {user.get('username', 'unknown') if user else 'unknown'}: {title[:60]}",
                links=[audit.job(job["uid"])],
            )
            raise

    def cleanup(self, job: dict) -> None:
        pass
