# -*- coding: utf-8 -*-
import json
import os
from odoo import models, fields, modules

class Website(models.Model):
    _inherit = 'website'

    enable_delivery_option = fields.Boolean(
        string='Enable Delivery Option',
        default=True,
        help='If disabled, customers will not see the Delivery option on the checkout page.'
    )

    def get_recent_blogs(self, limit=3):
        """Reads blog posts from blogs.json and returns the latest slice."""
        addon_path = modules.get_module_path('dine360_theme_chennora')
        json_path = os.path.join(addon_path, 'data', 'blogs.json')
        
        if os.path.exists(json_path):
            try:
                with open(json_path, 'r', encoding='utf-8-sig') as f:
                    blogs = json.load(f)
                    if isinstance(blogs, list):
                        return blogs[::-1][:limit]
                    return blogs[:limit] if isinstance(blogs, list) else []
            except Exception:
                return []
        return []
