from odoo import api, fields, models
from odoo.exceptions import UserError, AccessError, ValidationError
from datetime import datetime
from dateutil import relativedelta


class CustomProjectWizard(models.TransientModel):
    _name = 'project.revenue.wizard'

    project_id = fields.Many2one('project.project')
    #start_date = fields.Date(required=True)
    #end_date = fields.Datet(compute='_compute_end_date', store=True)
    #end_date = fields.Date()
    date = fields.Date(required=True)
    fixed_amount = fields.Float()

    # @api.depends('start_date')
    # def _compute_end_date(self):
    #     for rec in self:
    #         if rec.start_date:
    #             rec.end_date = rec.start_date + relativedelta.relativedelta(months=1) - relativedelta.relativedelta(
    #                 days=1)

    """@api.depends('start_date')
    def _compute_end_date(self):
        if self.start_date:
            self.end_date = self.start_date + relativedelta.relativedelta(months=1) - relativedelta.relativedelta(days=1)"""

    def action_set_revenue_lines(self):
        values = {
            'project_id':self.project_id.id,
            # 'start_date':self.start_date,
            # 'end_date':self.end_date,
            'date':self.date,
            'fixed_amount':self.fixed_amount
        }
        res = self.env['project.revenue.lines'].create(values)
        return res

    """@api.onchange('start_date')
    def _onchange_start_date(self):
        active_id = self._context.get('active_ids', [])
        project_object = self.env['project.project'].search([('id', '=', active_id[0])])
        records = self.env['project.revenue.lines'].sudo().search([('project_id', '=', project_object.id)])
        if self.start_date:
            if self.start_date.day != 1:
                raise AccessError('Please select first date of month')
            else:
                self.project_id = project_object.id
            for line in records:
                if self.start_date >= line.start_date and self.start_date <= line.end_date:
                    raise AccessError('Date already exist')"""
