from __future__ import annotations
from pydantic import BaseModel, Field
from typing import Optional


class Address(BaseModel):
    street: Optional[str] = None
    city: Optional[str] = None
    postal_code: Optional[str] = None
    region: Optional[str] = None
    country: Optional[str] = None


class Contacts(BaseModel):
    phone: Optional[str] = None
    fax: Optional[str] = None
    email: Optional[str] = None
    website: Optional[str] = None


class Director(BaseModel):
    name: Optional[str] = None
    role: Optional[str] = None
    appointed_on: Optional[str] = None
    resigned_on: Optional[str] = None


class PSC(BaseModel):
    name: Optional[str] = None
    natures_of_control: Optional[list[str]] = None
    notified_on: Optional[str] = None


class Company(BaseModel):
    tax_id: str
    country: str
    name: str
    address: Optional[Address] = None
    legal_form: Optional[str] = None
    status: Optional[str] = None
    incorporation_date: Optional[str] = None
    activity_code: Optional[str] = None
    activity_description: Optional[str] = None
    capital: Optional[float] = None
    capital_currency: Optional[str] = None
    contacts: Optional[Contacts] = None
    directors: Optional[list[Director]] = None
    psc: Optional[list[PSC]] = None
    vat_valid: Optional[bool] = None
    vat_consultation_number: Optional[str] = None
    source: str
    data_quality: str
    cached: bool
    stale: Optional[bool] = None
    data_age_days: Optional[int] = None
    fetched_at: str


class ValidationResult(BaseModel):
    tax_id: str
    country: str
    format_valid: bool
    vat_valid: Optional[bool] = None
    company_name: Optional[str] = None
    consultation_number: Optional[str] = None


class CountryInfo(BaseModel):
    code: str
    name: str
    enriched: bool
    source: Optional[str] = None
    vat_validation: bool


class CountriesResponse(BaseModel):
    total: int
    enriched: int
    vat_validation: int
    countries: list[CountryInfo]


class SourceHealth(BaseModel):
    status: str
    latency_ms: Optional[int] = None
    budget_remaining: Optional[int] = None


class CacheHealth(BaseModel):
    status: str


class HealthResponse(BaseModel):
    status: str
    timestamp: str
    sources: Optional[dict[str, SourceHealth]] = None
    cache: Optional[CacheHealth] = None


class BatchError(BaseModel):
    tax_id: str
    error: str
    code: str


class BatchResponse(BaseModel):
    total: int
    successful: int
    results: list[Company | BatchError]
