"""Tests for the To-do integration."""

import dataclasses
import uuid

from homeassistant.components.todo import DOMAIN, TodoItem, TodoListEntity
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from tests.common import MockConfigEntry, MockPlatform, mock_platform

TEST_DOMAIN = "test"


class MockFlow(ConfigFlow):
    """Test flow."""


class MockTodoListEntity(TodoListEntity):
    """Test todo list entity."""

    def __init__(self, items: list[TodoItem] | None = None) -> None:
        """Initialize entity."""
        self._attr_todo_items = items

    @property
    def items(self) -> list[TodoItem]:
        """Return the items in the To-do list."""
        return self._attr_todo_items or []

    async def async_create_todo_item(self, item: TodoItem) -> None:
        """Add an item to the To-do list."""
        if self._attr_todo_items is None:
            self._attr_todo_items = []
        self._attr_todo_items.append(
            dataclasses.replace(item, uid=item.uid or uuid.uuid4().hex)
        )

    async def async_delete_todo_items(self, uids: list[str]) -> None:
        """Delete an item in the To-do list."""
        self._attr_todo_items = [item for item in self.items if item.uid not in uids]

    async def async_update_todo_item(self, item: TodoItem) -> None:
        """Update an item in the To-do list."""
        for idx, existing_item in enumerate(self.items):
            if existing_item.uid == item.uid:
                assert self._attr_todo_items is not None
                self._attr_todo_items[idx] = item
                break


async def create_mock_platform(
    hass: HomeAssistant,
    entities: list[TodoListEntity],
) -> MockConfigEntry:
    """Create a todo platform with the specified entities."""

    async def async_setup_entry_platform(
        hass: HomeAssistant,
        config_entry: ConfigEntry,
        async_add_entities: AddConfigEntryEntitiesCallback,
    ) -> None:
        """Set up test event platform via config entry."""
        async_add_entities(entities)

    mock_platform(
        hass,
        f"{TEST_DOMAIN}.{DOMAIN}",
        MockPlatform(async_setup_entry=async_setup_entry_platform),
    )

    config_entry = MockConfigEntry(domain=TEST_DOMAIN)
    config_entry.add_to_hass(hass)
    assert await hass.config_entries.async_setup(config_entry.entry_id)
    await hass.async_block_till_done()

    return config_entry
