"""Service layer for CoordComponentState."""

from typing import Optional

from fastapi import HTTPException, status
from sqlalchemy.dialects.mysql import insert
from sqlalchemy.orm import Session

from api.models.coord_component_state import CoordComponentState
from api.schemas.coord_component_state import CoordComponentStateUpsert


def get_component_states(
    db: Session,
    project_key: Optional[str] = None,
) -> list[CoordComponentState]:
    """Return all component states, optionally filtered by project."""
    q = db.query(CoordComponentState)
    if project_key:
        q = q.filter(CoordComponentState.project_key == project_key)
    return q.order_by(CoordComponentState.project_key, CoordComponentState.component).all()


def upsert_component_state(
    db: Session,
    project_key: str,
    component: str,
    data: CoordComponentStateUpsert,
) -> CoordComponentState:
    """Insert or update a component state using MariaDB ON DUPLICATE KEY UPDATE."""
    try:
        stmt = insert(CoordComponentState).values(
            project_key=project_key,
            component=component,
            state=data.state,
            version=data.version,
            notes=data.notes,
            updated_by=data.updated_by,
        )
        stmt = stmt.on_duplicate_key_update(
            state=stmt.inserted.state,
            version=stmt.inserted.version,
            notes=stmt.inserted.notes,
            updated_by=stmt.inserted.updated_by,
        )
        db.execute(stmt)
        db.commit()

        record = (
            db.query(CoordComponentState)
            .filter(
                CoordComponentState.project_key == project_key,
                CoordComponentState.component == component,
            )
            .first()
        )
        return record
    except Exception as e:
        db.rollback()
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to upsert component state: {e}"
        )
