from odoo import api, models, _

from odoo.exceptions import AccessError, MissingError, ValidationError


class ServicesReport(models.AbstractModel):
    _name = "report.fleet_advanced_reports.report_fleet_services"
    _description = "Services PDF Report"

    def _get_report_values(self, docids, data=None):
        service_id = data.get("service_id")
        vehicle_id = data.get("vehicle_id")
        date_from = data.get("date_from")
        date_to = data.get("date_to")

        # Build the domain for the search based on the selected filters
        domain = []
        if service_id:
            domain = [("service_type_id", "=", service_id)]
        if vehicle_id:
            domain.append(("vehicle_id", "=", vehicle_id))
        domain += [("date", ">=", str(date_from)), ("date", "<=", str(date_to))]

        # Fetch the records based on the domain
        service_records = self.env["fleet.vehicle.log.services"].search(domain)

        if not service_records:
            raise ValidationError(
                _("There are no service records matching the given filters.")
            )

        print(
            ">>>>>>>>>>>> Moeda: ", self.env.ref("base.main_company").currency_id.symbol
        )

        return {
            "services": service_records,
            "period": "From " + str(date_from) + " to " + str(date_to),
            "current_currency": self.env.ref("base.main_company").currency_id.symbol,
        }
