# -*- coding: utf-8 -*-
from odoo import http
from odoo.http import request
from odoo.addons.web.controllers.home import Home
from odoo.addons.website.controllers.main import Website
from odoo.addons.dine360_dashboard.controllers.main import render_dashboard

class CustomHomeWebsite(Home):
    @http.route('/web/login', type='http', auth="public", website=True)
    def web_login(self, *args, **kw):
        response = super(CustomHomeWebsite, self).web_login(*args, **kw)
        if request.params.get('login_success') and request.session.uid:
            return request.redirect('/')
        return response

class ImageHomeWebsite(Website):
    @http.route('/', type='http', auth='public', website=True, sitemap=True)
    def index(self, **kwargs):
        # 1. If not logged in, show website homepage
        if not request.session.uid:
            return super(ImageHomeWebsite, self).index(**kwargs)

        # 2. If logged in, check for editor / iframe detection
        params = request.params
        headers = request.httprequest.headers
        referer = headers.get('Referer', '')
        fetch_dest = headers.get('Sec-Fetch-Dest', '')
        
        editor_params = ['enable_editor', 'edit', 'path', 'website_id', 'frontend_edit', 'model', 'id']
        is_editor_request = any(p in params for p in editor_params)
        is_from_backend = any(m in referer for m in ['/website/force', 'enable_editor'])

        if fetch_dest == 'iframe' or is_editor_request or is_from_backend:
            return super(ImageHomeWebsite, self).index(**kwargs)

        if request.httprequest.path != '/':
            return super(ImageHomeWebsite, self).index(**kwargs)

        # Render the dashboard
        return render_dashboard(request)

    @http.route('/home', type='http', auth="public", website=True, sitemap=True)
    def website_home(self, **kw):
        # Explicit route for standard Website Homepage
        return request.render('website.homepage')
