"""Shared fixtures for LAPIS converter tests."""

from __future__ import annotations

from pathlib import Path

import pytest

FIXTURES_DIR = Path(__file__).parent / "e2e" / "fixtures"


@pytest.fixture()
def fixtures_dir() -> Path:
    return FIXTURES_DIR


@pytest.fixture()
def minimal_openapi() -> dict:
    """A minimal valid OpenAPI 3.0 document."""
    return {
        "openapi": "3.0.3",
        "info": {
            "title": "Minimal API",
            "version": "1.0.0",
        },
        "servers": [{"url": "https://api.example.com"}],
        "paths": {
            "/health": {
                "get": {
                    "operationId": "health_check",
                    "summary": "Health check endpoint",
                    "responses": {
                        "200": {
                            "description": "OK",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "status": {"type": "string"},
                                        },
                                    }
                                }
                            },
                        }
                    },
                }
            }
        },
    }


@pytest.fixture()
def invoice_openapi() -> dict:
    """OpenAPI document matching the Invoice Service example from the spec."""
    return {
        "openapi": "3.1.0",
        "info": {
            "title": "Invoice Service",
            "version": "2.2.0",
            "description": "API para gestion de facturas, clientes y pagos.",
        },
        "servers": [{"url": "https://api.example.com/v2"}],
        "security": [{"bearerAuth": []}],
        "paths": {
            "/invoices": {
                "post": {
                    "operationId": "create_invoice",
                    "summary": "Crea factura para un cliente.",
                    "requestBody": {
                        "required": True,
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "required": ["customer_id", "lines"],
                                    "properties": {
                                        "customer_id": {"type": "string"},
                                        "lines": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/components/schemas/InvoiceLine"
                                            },
                                        },
                                        "billing_address": {
                                            "$ref": "#/components/schemas/Address"
                                        },
                                        "due_date": {
                                            "type": "string",
                                            "format": "date",
                                        },
                                        "notes": {"type": "string"},
                                    },
                                }
                            }
                        },
                    },
                    "responses": {
                        "201": {
                            "description": "Created",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/Invoice"
                                    }
                                }
                            },
                        },
                        "400": {
                            "description": "Invalid input",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/ApiError"
                                    }
                                }
                            },
                        },
                        "401": {
                            "description": "Unauthorized",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/ApiError"
                                    }
                                }
                            },
                        },
                    },
                },
                "get": {
                    "operationId": "list_invoices",
                    "summary": "Lista facturas con filtros.",
                    "x-paginated": True,
                    "parameters": [
                        {
                            "name": "customer_id",
                            "in": "query",
                            "schema": {"type": "string"},
                        },
                        {
                            "name": "status",
                            "in": "query",
                            "schema": {
                                "type": "string",
                                "enum": ["draft", "sent", "paid", "overdue", "cancelled"],
                            },
                        },
                    ],
                    "responses": {
                        "200": {
                            "description": "OK",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "items": {
                                                "type": "array",
                                                "items": {
                                                    "$ref": "#/components/schemas/Invoice"
                                                },
                                            },
                                            "total_count": {"type": "integer"},
                                        },
                                    }
                                }
                            },
                        },
                        "401": {
                            "description": "Unauthorized",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/ApiError"
                                    }
                                }
                            },
                        },
                    },
                },
            },
            "/invoices/{invoice_id}": {
                "get": {
                    "operationId": "get_invoice",
                    "summary": "Detalle completo de factura.",
                    "parameters": [
                        {
                            "name": "invoice_id",
                            "in": "path",
                            "required": True,
                            "schema": {"type": "string"},
                        }
                    ],
                    "responses": {
                        "200": {
                            "description": "OK",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/Invoice"
                                    }
                                }
                            },
                        },
                        "404": {
                            "description": "Not found",
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "$ref": "#/components/schemas/ApiError"
                                    }
                                }
                            },
                        },
                    },
                }
            },
        },
        "webhooks": {
            "invoicePaid": {
                "post": {
                    "operationId": "invoice_paid",
                    "summary": "Notifica cuando una factura se paga.",
                    "requestBody": {
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "event_id": {"type": "string"},
                                        "invoice_id": {"type": "string"},
                                        "amount": {"type": "number", "format": "float"},
                                        "paid_at": {
                                            "type": "string",
                                            "format": "date-time",
                                        },
                                    },
                                }
                            }
                        }
                    },
                    "responses": {"200": {"description": "OK"}},
                }
            }
        },
        "components": {
            "securitySchemes": {
                "bearerAuth": {
                    "type": "http",
                    "scheme": "bearer",
                    "bearerFormat": "JWT",
                }
            },
            "schemas": {
                "ApiError": {
                    "type": "object",
                    "properties": {
                        "code": {"type": "string"},
                        "message": {"type": "string"},
                        "request_id": {"type": "string"},
                        "details": {"nullable": True},
                    },
                },
                "Address": {
                    "type": "object",
                    "required": ["street", "city", "zip"],
                    "properties": {
                        "street": {"type": "string"},
                        "city": {"type": "string"},
                        "zip": {"type": "string"},
                        "country": {"type": "string", "default": "US"},
                    },
                },
                "InvoiceLine": {
                    "type": "object",
                    "required": ["product_id", "quantity", "unit_price"],
                    "properties": {
                        "product_id": {"type": "string"},
                        "description": {"type": "string"},
                        "quantity": {"type": "integer"},
                        "unit_price": {"type": "number", "format": "float"},
                    },
                },
                "Invoice": {
                    "type": "object",
                    "properties": {
                        "id": {"type": "string"},
                        "customer_id": {"type": "string"},
                        "status": {
                            "type": "string",
                            "enum": [
                                "draft",
                                "sent",
                                "paid",
                                "overdue",
                                "cancelled",
                            ],
                        },
                        "lines": {
                            "type": "array",
                            "items": {
                                "$ref": "#/components/schemas/InvoiceLine"
                            },
                        },
                        "billing_address": {
                            "$ref": "#/components/schemas/Address"
                        },
                        "subtotal": {"type": "number", "format": "float"},
                        "tax": {"type": "number", "format": "float"},
                        "total": {"type": "number", "format": "float"},
                        "created_at": {
                            "type": "string",
                            "format": "date-time",
                        },
                        "due_date": {"type": "string", "format": "date"},
                        "paid_at": {
                            "type": "string",
                            "format": "date-time",
                        },
                        "metadata": {
                            "type": "object",
                            "additionalProperties": True,
                            "x-since": "2.1",
                        },
                    },
                },
            },
        },
    }
