# Copyright 2026 Criptomart
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)

from odoo import fields
from odoo import models

# Pylint: explicit 'string' attributes are intentional for readable labels in views.
# Some linters flag these as redundant; disable that specific check here.
# pylint: disable=attribute-string-redundant


class GroupOrderSlot(models.Model):
    _name = "group.order.slot"
    _description = "Pickup slot for a Consumer Group Order"
    _order = "sequence, weekday, start_hour"

    group_order_id = fields.Many2one(
        "group.order",
        string="Group Order",
        required=True,
        ondelete="cascade",
        help="Consumer group order this slot belongs to",
    )

    weekday = fields.Selection(
        [(str(i), str(i)) for i in range(7)],
        string="Weekday",
        required=True,
        help="Day of week for this slot (0=Monday)",
    )

    start_hour = fields.Float(
        string="Start hour",
        help="Start hour in decimal form, e.g. 9.5 = 09:30",
    )

    end_hour = fields.Float(
        string="End hour",
        help="End hour in decimal form, e.g. 14.25 = 14:15",
    )

    label = fields.Char(
        string="Label",
        help="Human readable short label for the slot (optional)",
    )

    sequence = fields.Integer(string="Sequence", default=10)

    active = fields.Boolean(default=True)

    def _get_display_label(self):
        """Return a fallback display label combining weekday and hours.

        This is a small helper used by views or when a specific `label` is
        not provided.
        """
        self.ensure_one()
        if self.label:
            return self.label
        # Fallback: simple numeric representation
        sh = "%02d:%02d" % (
            int(self.start_hour or 0),
            int((self.start_hour or 0) % 1 * 60),
        )
        eh = "%02d:%02d" % (int(self.end_hour or 0), int((self.end_hour or 0) % 1 * 60))
        return f"{self.weekday} {sh}-{eh}"
