from pydantic import BaseModel
from typing import List, Optional


class BuiltMelodyCreate(BaseModel):
    name: str
    pid: str
    steps: str  # raw step string e.g. "1,2,2+1,1,2,3+1"
    is_builtin: bool = False


class BuiltMelodyUpdate(BaseModel):
    name: Optional[str] = None
    pid: Optional[str] = None
    steps: Optional[str] = None
    is_builtin: Optional[bool] = None


class BuiltMelodyInDB(BaseModel):
    id: str
    name: str
    pid: str
    steps: str
    is_builtin: bool = False
    binary_path: Optional[str] = None
    binary_url: Optional[str] = None
    progmem_code: Optional[str] = None
    assigned_melody_ids: List[str] = []
    created_at: str
    updated_at: str

    @property
    def step_count(self) -> int:
        if not self.steps:
            return 0
        return len(self.steps.split(","))


class BuiltMelodyListResponse(BaseModel):
    melodies: List[BuiltMelodyInDB]
    total: int
