import logging
from dataclasses import dataclass

from fasthtml import serve
from fasthtml.components import *

from myfasthtml.controls.helpers import mk
from myfasthtml.core.bindings import Binding, BooleanConverter
from myfasthtml.core.utils import debug_routes
from myfasthtml.myfastapp import create_app

logging.basicConfig(
  level=logging.DEBUG,  # Set logging level to DEBUG
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',  # Log format
  datefmt='%Y-%m-%d %H:%M:%S',  # Timestamp format
)

app, rt = create_app(protect_routes=False)


@dataclass
class Data:
  value: str = "Hello World"
  checked: bool = False


data = Data()


@rt("/set_checkbox")
def post(check_box_name: str = None):
  print(check_box_name)


@rt("/")
def index():
  return Div(
    mk.mk(Input(name="checked_name", type="checkbox"), binding=Binding(data, attr="checked")),
    mk.mk(Label("Text"), binding=Binding(data, attr="checked", converter=BooleanConverter())),
  )


@rt("/test_checkbox_htmx")
def get():
  check_box = Input(type="checkbox", name="check_box_name", hx_post="/set_checkbox")
  return check_box


if __name__ == "__main__":
  debug_routes(app)
  serve(port=5002)
