from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from interface import Form
    from router import Event

class Module:
    @loader.command("panel", source="both", access="owner")
    async def panel(self, ctx: Event) -> None:
        count = await app.db.get("count", 0)
        form = app.inline(ctx)
        form.state["count"] = count
        keyboard = app.inline.keyboard([
            [app.inline.button(self.label, self.increment, key="increment", args=(1,))],
            [app.inline.button("Документация", key="docs", url="https://cluster.ninepeaks.dev/developers/")],
            [app.inline.button("Закрыть", self.close, key="close")],
        ])
        await form.form(text=app.text().bold("Счётчик").line(str(count)), keyboard=keyboard)

    def label(self, form: Form) -> str: return f"Прибавить: {form.state.get('count', 0)}"

    @loader.callback(access="owner", fields={"amount": {"type": "integer", "min": 1, "max": 100, "default": 1}})
    async def increment(self, ctx: Event) -> None:
        count = await app.db.get("count", 0) + ctx.values["amount"]
        await app.db.set("count", count)
        form = ctx.form
        if not form.data: await form.load()
        form.state["count"] = count
        form.button("increment").rename(self.label)
        form.button("docs").hide()
        await form.edit(text=app.text().bold("Счётчик").line(str(count)))
        await ctx.answer("Обновлено")

    @loader.callback(access="owner")
    async def close(self, ctx: Event) -> None:
        await ctx.form.close()
