"""
Contract Tests: Pets API

Endpoint: /mascotas/api/v1/pets/
App: mascotas

Related Tickets:
- VET-537: Paso 1 - Test creación de la mascota vinculada al petowner invitado
- VET-535: Establecer y definir test para las apis vinculadas al procesos de solicitar turno general

Context: In the turnero general flow (Step 1), a pet is created and linked to the guest
pet owner. The pet data (type, name, neutered status) combined with the owner's address
is used to filter available services and veterinarians.
"""

from ..base import ContractTestCase
from ..endpoints import Endpoints
from ..helpers import (
    sample_pet_owner,
    unique_email,
    SAMPLE_CAT,
    SAMPLE_DOG,
    SAMPLE_NEUTERED_CAT,
)


class TestPetCreate(ContractTestCase):
    """POST /mascotas/api/v1/pets/

    VET-537: Tests for pet creation linked to guest petowner (Step 1 of turnero flow)
    """

    def _create_owner(self):
        """Helper to create a pet owner"""
        data = sample_pet_owner(unique_email("pet_owner"))
        response = self.post(Endpoints.PET_OWNERS, data)
        return response.data["id"]

    def test_create_cat_returns_201(self):
        """
        Creating a cat returns 201 with pet_type CAT.

        Request (from production turnero):
            POST /mascotas/api/v1/pets/
            {
                "name": "Luna",
                "pet_type": "CAT",
                "is_neutered": false,
                "owner": 12345
            }

        Response (201):
            {
                "id": 67890,
                "name": "Luna",
                "pet_type": "CAT",
                "is_neutered": false,
                "owner": 12345,
                "breed": null,
                "birth_date": null,
                "created_at": "2024-12-11T15:31:15.123Z"
            }
        """
        owner_id = self._create_owner()
        data = {**SAMPLE_CAT, "owner": owner_id}

        response = self.post(Endpoints.PETS, data)

        self.assert_status(response, 201)
        self.assert_has_fields(response.data, "id", "name", "pet_type", "owner")
        self.assertEqual(response.data["pet_type"], "CAT")
        self.assertEqual(response.data["name"], "TestCat")

    def test_create_dog_returns_201(self):
        """
        Creating a dog returns 201 with pet_type DOG.

        Validates that both major pet types (CAT/DOG) are supported in the contract.
        """
        owner_id = self._create_owner()
        data = {**SAMPLE_DOG, "owner": owner_id}

        response = self.post(Endpoints.PETS, data)

        self.assert_status(response, 201)
        self.assertEqual(response.data["pet_type"], "DOG")

    def test_neutered_status_persisted(self):
        """
        Neutered status is persisted correctly.

        This is important business data that may affect service recommendations
        or veterinarian assignments.
        """
        owner_id = self._create_owner()
        data = {**SAMPLE_NEUTERED_CAT, "owner": owner_id}

        response = self.post(Endpoints.PETS, data)

        self.assert_status(response, 201)
        self.assertTrue(response.data["is_neutered"])

    def test_requires_owner(self):
        """
        Pet creation without owner should fail.

        Enforces the required link between pet and petowner - critical for the
        turnero flow where pets must be associated with the guest user.
        """
        data = SAMPLE_CAT.copy()

        response = self.post(Endpoints.PETS, data)

        self.assert_status(response, 400)

    def test_invalid_pet_type_rejected(self):
        """
        Invalid pet_type should be rejected.

        Currently only CAT and DOG are supported. This test ensures the contract
        validates pet types correctly.
        """
        owner_id = self._create_owner()
        data = {
            "name": "InvalidPet",
            "pet_type": "HAMSTER",
            "owner": owner_id,
        }

        response = self.post(Endpoints.PETS, data)

        self.assert_status(response, 400)


class TestPetRetrieve(ContractTestCase):
    """GET /mascotas/api/v1/pets/{id}/"""

    def _create_owner_with_pet(self):
        """Helper to create owner and pet"""
        owner_data = sample_pet_owner(unique_email("pet_owner"))
        owner_response = self.post(Endpoints.PET_OWNERS, owner_data)
        owner_id = owner_response.data["id"]

        pet_data = {**SAMPLE_CAT, "owner": owner_id}
        pet_response = self.post(Endpoints.PETS, pet_data)
        return pet_response.data["id"]

    def test_get_by_id_returns_200(self):
        """GET pet by ID returns pet details"""
        pet_id = self._create_owner_with_pet()

        response = self.get(Endpoints.PET_DETAIL.format(id=pet_id))

        self.assert_status(response, 200)
        self.assertEqual(response.data["id"], pet_id)

    def test_nonexistent_returns_404(self):
        """GET non-existent pet returns 404"""
        response = self.get(Endpoints.PET_DETAIL.format(id=999999))

        self.assert_status(response, 404)


class TestPetList(ContractTestCase):
    """GET /mascotas/api/v1/pets/"""

    def test_list_returns_200(self):
        """GET pets list returns 200 (with pagination)"""
        response = self.get(Endpoints.PETS, params={"page_size": 1})

        self.assert_status(response, 200)
