"""Tests unitaires pour le module app.fiches.utils.tickets.core.

Ces tests vérifient les fonctions de gestion des tickets Gitea.
"""

import os
from unittest.mock import Mock, mock_open, patch

import pytest
import requests

from app.fiches.utils.tickets.core import (
    charger_fiches_et_labels,
    construire_corps_ticket_markdown,
    creer_ticket_gitea,
    get_labels_existants,
    gitea_request,
    nettoyer_labels,
    rechercher_tickets_gitea,
)


class TestGiteaRequest:
    """Tests pour la fonction gitea_request."""

    @patch("app.fiches.utils.tickets.core.requests.request")
    def test_requete_get_succes(self, mock_request):
        """Test une requête GET réussie."""
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.json.return_value = {"data": "test"}
        mock_request.return_value = mock_response

        resultat = gitea_request("get", "https://gitea.example.com/api/v1/repos")

        assert resultat is not None
        assert resultat.status_code == 200
        mock_request.assert_called_once()

    @patch("app.fiches.utils.tickets.core.requests.request")
    def test_ajout_automatique_token(self, mock_request):
        """Test que le token est automatiquement ajouté aux headers."""
        mock_response = Mock()
        mock_response.status_code = 200
        mock_request.return_value = mock_response

        gitea_request("get", "https://gitea.example.com/api/v1/repos")

        # Vérifier que Authorization est dans les headers
        call_kwargs = mock_request.call_args[1]
        assert "Authorization" in call_kwargs["headers"]
        assert call_kwargs["headers"]["Authorization"].startswith("token ")


class TestChargerFichesEtLabels:
    """Tests pour la fonction charger_fiches_et_labels."""

    def test_chargement_csv_valide(self, tmp_path):
        """Test le chargement d'un fichier CSV valide."""
        csv_content = """Fiche,Label opération,Label item
Processeur,Assemblage / Fabrication,Composant
Lithium,Extraction / Traitement,Minerai
"""
        assets_dir = tmp_path / "assets"
        assets_dir.mkdir()
        csv_file = assets_dir / "fiches_labels.csv"
        csv_file.write_text(csv_content, encoding="utf-8")

        with patch("os.path.join", return_value=str(csv_file)):
            with patch("builtins.open", mock_open(read_data=csv_content)):
                resultat = charger_fiches_et_labels()

                assert "Processeur" in resultat
                assert resultat["Processeur"]["operations"] == ["Assemblage", "Fabrication"]
                assert resultat["Processeur"]["item"] == "Composant"


class TestRechercherTicketsGitea:
    """Tests pour la fonction rechercher_tickets_gitea."""

    @patch("app.fiches.utils.tickets.core.gitea_request")
    @patch("app.fiches.utils.tickets.core.charger_fiches_et_labels")
    @patch("app.fiches.utils.tickets.core.ENV", "dev")
    def test_recherche_tickets_existants(self, mock_charger_fiches, mock_gitea):
        """Test la recherche de tickets pour une fiche."""
        mock_charger_fiches.return_value = {
            "Processeur": {
                "operations": ["Assemblage", "Fabrication"],
                "item": "Composant"
            }
        }

        mock_response = Mock()
        mock_response.json.return_value = [
            {
                "id": 123,
                "title": "Test issue",
                "state": "open",
                "ref": "refs/heads/dev",
                "labels": [
                    {"name": "Assemblage"},
                    {"name": "Composant"}
                ]
            }
        ]
        mock_gitea.return_value = mock_response

        resultat = rechercher_tickets_gitea("Processeur")

        assert len(resultat) > 0
        assert resultat[0]["id"] == 123


class TestGetLabelsExistants:
    """Tests pour la fonction get_labels_existants."""

    @patch("app.fiches.utils.tickets.core.gitea_request")
    def test_recuperation_labels(self, mock_gitea):
        """Test la récupération des labels existants."""
        mock_response = Mock()
        mock_response.json.return_value = [
            {"name": "Assemblage", "id": 1, "color": "#ff0000"},
            {"name": "Fabrication", "id": 2, "color": "#00ff00"},
            {"name": "Composant", "id": 3, "color": "#0000ff"}
        ]
        mock_gitea.return_value = mock_response

        resultat = get_labels_existants()

        assert "Assemblage" in resultat
        assert resultat["Assemblage"] == 1


class TestNettoyerLabels:
    """Tests pour la fonction nettoyer_labels."""

    def test_nettoyage_labels(self):
        """Test le nettoyage et tri des labels."""
        labels = ["Assemblage", "  Composant  ", "Assemblage", "Fabrication"]

        resultat = nettoyer_labels(labels)

        assert "Assemblage" in resultat
        assert "Composant" in resultat
        assert resultat.count("Assemblage") == 1


class TestConstruireCorpsTicketMarkdown:
    """Tests pour la fonction construire_corps_ticket_markdown."""

    def test_construction_corps_simple(self):
        """Test la construction du corps markdown."""
        reponses = {
            "Description": "Description de test",
            "Contexte": "Contexte du ticket"
        }

        resultat = construire_corps_ticket_markdown(reponses)

        assert "Description" in resultat
        assert "Description de test" in resultat
        assert "##" in resultat


class TestCreerTicketGitea:
    """Tests pour la fonction creer_ticket_gitea."""

    @patch("app.fiches.utils.tickets.core.gitea_request")
    def test_creation_ticket_succes(self, mock_gitea):
        """Test la création réussie d'un ticket."""
        mock_response = Mock()
        mock_response.status_code = 201
        mock_gitea.return_value = mock_response

        resultat = creer_ticket_gitea("Test Ticket", "Corps du ticket", [1, 2])

        assert resultat is True
