from fastapi import Request
from framefox.core.controller.abstract_controller import AbstractController
from framefox.core.routing.decorator.route import Route
from src.repository.{{ repository_file_name }} import {{ repository_class_name }}
from src.entity.{{ entity_file_name }} import {{ entity_class_name }}
from framefox.core.orm.entity_manager_interface import EntityManagerInterface
from src.form.{{ entity_name }}_type import {{ entity_class_name }}Type
from starlette.responses import HTMLResponse, RedirectResponse


class {{ controller_class_name }}(AbstractController):
    def __init__(self, entityManager: EntityManagerInterface):
        self.entity_manager = entityManager
        self.repository = {{ repository_class_name }}()

    @Route("/{{ entity_name }}s", "{{ entity_file_name }}.read_all", methods=["GET"])
    async def read_all(self) -> HTMLResponse:
        items = self.repository.find_all()
        return self.render("{{ entity_name }}/index.html", {"items": items})

    @Route("/{{ entity_name }}/create", "{{ entity_name }}.create", methods=["GET", "POST"])
    async def create(self, request: Request) -> HTMLResponse:
        {{ entity_name }} = {{ entity_class_name }}()
        form = self.create_form({{ entity_class_name }}Type, {{ entity_name }})
        await form.handle_request(request)
        if form.is_submitted() and form.is_valid():
            self.entity_manager.persist({{ entity_name }})
            self.entity_manager.commit()
            self.flash("success", "{{ entity_class_name }} created successfully!")
            return self.redirect(self.generate_url("{{ entity_file_name }}.read_all"))
        return self.render("{{ entity_name }}/create.html", {
            "form": form.create_view()
        })

    @Route("/{{ entity_name }}/{id}", "{{ entity_file_name }}.read", methods=["GET"])
    async def read(self, id: int) -> HTMLResponse:
        item = self.repository.find(id)
        if not item:
            self.flash("error", "{{ entity_class_name }} not found!")
            return self.redirect(self.generate_url("{{ entity_file_name }}.read_all"))
        return self.render("{{ entity_name }}/read.html", {"item": item})

    @Route("/{{ entity_name }}/{id}/update", "{{ entity_file_name }}.update", methods=["GET", "POST"])
    async def update(self, request: Request, id: int) -> HTMLResponse:
        {{ entity_name }} = self.repository.find(id)
        form = self.create_form({{ entity_class_name }}Type, {{ entity_name }})

        await form.handle_request(request)
        if form.is_submitted() and form.is_valid():
            self.entity_manager.persist({{ entity_name }})
            self.entity_manager.commit()
            self.flash("success", "{{ entity_class_name }} updated successfully!")
            return self.redirect(self.generate_url("{{ entity_file_name }}.read_all"))

        return self.render("{{ entity_name }}/update.html", {
            "form": form.create_view(),
            "item": {{ entity_name }}  # Pour compatibilité avec les templates existants
        })

    @Route("/{{ entity_name }}/delete/{id}", "{{ entity_file_name }}.delete", methods=["POST"])
    async def delete(self, id: int) -> RedirectResponse:
        try:
            {{ entity_name }} = self.repository.find(id)
            if not {{ entity_name }}:
                self.flash("error", "{{ entity_class_name }} not found!")
                return self.redirect(self.generate_url("{{ entity_file_name }}.read_all"))
            self.entity_manager.delete({{ entity_name }})
            self.entity_manager.commit()
            self.flash("success", "{{ entity_class_name }} deleted successfully!")
            return self.redirect(self.generate_url("{{ entity_file_name }}.read_all"))
        except Exception as e:
            self.flash("error", str(e))
            return self.redirect(self.generate_url("{{ entity_file_name }}.read_all"))