# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo.http import Controller, request, route


class DashboardDataRoute(Controller):
    @route(
        ['/spreadsheet/dashboard/data/<model("spreadsheet.dashboard"):dashboard>'],
        type='http',
        auth='user',
        readonly=True,
    )
    def get_dashboard_data(self, dashboard):
        dashboard = dashboard.exists()
        if not dashboard:
            raise request.not_found()
        if dashboard.sample_dashboard_file_path and dashboard._dashboard_is_empty():
            sample_data = dashboard._get_sample_dashboard()
            if sample_data:
                return request.make_json_response({
                    'snapshot': sample_data,
                    'is_sample': True,
                })
        body = dashboard._get_serialized_readonly_dashboard()
        headers = [
            ('Content-Length', len(body)),
            ('Content-Type', 'application/json; charset=utf-8'),
        ]
        return request.make_response(body, headers)
