"""
Data models for PowerBI MCP Server

Pydantic models for request/response validation and structured output.
"""

from enum import Enum
from typing import Any
from pydantic import BaseModel, Field, field_validator

from .validation import validate_dax_query as _validate_dax_query


class ResponseFormat(str, Enum):
    """Output format for tool responses"""
    JSON = "json"
    MARKDOWN = "markdown"


class DetailLevel(str, Enum):
    """Level of detail in responses"""
    CONCISE = "concise"
    DETAILED = "detailed"


class Workspace(BaseModel):
    """PowerBI workspace (group) model"""
    id: str = Field(..., description="Unique workspace identifier")
    name: str = Field(..., description="Workspace name")
    type: str | None = Field(None, description="Workspace type")
    state: str | None = Field(None, description="Workspace state")
    is_read_only: bool | None = Field(None, alias="isReadOnly", description="Read-only flag")
    is_on_dedicated_capacity: bool | None = Field(
        None, alias="isOnDedicatedCapacity", description="Dedicated capacity flag"
    )
    capacity_id: str | None = Field(None, alias="capacityId", description="Capacity ID")

    model_config = {"populate_by_name": True}


class Dataset(BaseModel):
    """PowerBI dataset model"""
    id: str = Field(..., description="Unique dataset identifier")
    name: str = Field(..., description="Dataset name")
    configured_by: str | None = Field(None, alias="configuredBy", description="User who configured the dataset")
    is_refreshable: bool | None = Field(None, alias="isRefreshable", description="Refreshable flag")
    is_effective_identity_required: bool | None = Field(
        None, alias="isEffectiveIdentityRequired", description="Effective identity requirement"
    )
    is_effective_identity_roles_required: bool | None = Field(
        None, alias="isEffectiveIdentityRolesRequired", description="Effective identity roles requirement"
    )
    target_storage_mode: str | None = Field(None, alias="targetStorageMode", description="Storage mode")
    created_date: str | None = Field(None, alias="createdDate", description="Creation timestamp")

    model_config = {"populate_by_name": True}


class DAXQuery(BaseModel):
    """DAX query request model"""
    query: str = Field(..., description="DAX query expression (must start with EVALUATE)")

    @field_validator("query")
    @classmethod
    def validate_dax_query(cls, v: str) -> str:
        """Validate that DAX query starts with EVALUATE"""
        _validate_dax_query(v)
        return v


class QueryResult(BaseModel):
    """Query execution result model"""
    tables: list[dict[str, Any]] = Field(default_factory=list, description="Result tables")


class WorkspaceList(BaseModel):
    """List of workspaces response"""
    workspaces: list[Workspace] = Field(..., description="List of workspaces")
    total: int = Field(..., description="Total count of workspaces")


class DatasetList(BaseModel):
    """List of datasets response"""
    datasets: list[Dataset] = Field(..., description="List of datasets")
    total: int = Field(..., description="Total count of datasets")
