# -*- coding: utf-8 -*-

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

#class Project(models.Model):
    #_inherit = 'project.project'

    #project_cons_hrs = fields.One2many('project.consultant.hrs', 'project_id', 'Project Hours')

class ProjectConsultantHrs(models.Model):
    _name = 'project.consultant.hrs'
    _description = 'Project Consultant Hours'
    _order = 'employee_id, end_date desc'

    project_id = fields.Many2one('project.project', string="Project", required=True)
    employee_id = fields.Many2one('hr.employee', string="Consultant", required=False)
    start_date = fields.Date('Start Date', required=True)
    end_date = fields.Date('End Date', required=True)
    percentage = fields.Float("Budgeted Percentage (%)")
    budgeted_hours = fields.Float("Budgeted Hours for period", compute='_compute_budgeted_hours', store=True)
    actual_percentage = fields.Float("Actual Percentage (%)", compute='_compute_actual_calc', store=True)
    actual_hours = fields.Float("Actual Hours for period", compute='_compute_actual_calc', store=True)
    #hours_type = fields.Selection([('Budgeted Hours', 'Budgeted Hours'), ('Actual Hours', 'Actual Hours')], default="Budgeted Hours", string="Type")

    @api.constrains('start_date', 'end_date')
    def _check_dates(self):
        for rec in self:
            if rec.end_date < rec.start_date:
                raise ValidationError(_('Start date must be earlier than end date.'))

    @api.constrains('employee_id', 'percentage')
    def _check_percent(self):
        for val in self:
            if not val.employee_id and val.percentage:
                rec2 = val.search([('employee_id','=',False),('project_id','=',val.project_id.id)])
                #print("rec2", rec2)
                per2 = [r2.percentage for r2 in rec2]
                #print("perccccccc22222222", per2)
                if sum(per2) > 100:
                    raise ValidationError(_('Non Consultant total percentage should not be greater than 100'))
            if val.employee_id and val.percentage:
                rec = val.search([('employee_id','=',val.employee_id.id),('project_id','=',val.project_id.id)])
                per = [r.percentage for r in rec]
                if sum(per) > 100:
                    raise ValidationError(_('Consultant total percentage should not be greater than 100'))
            if (val.percentage <= 0.0 or val.percentage > 100.0):
                raise ValidationError(_('Percentage must be between 1 and 100.'))

    @api.constrains('start_date', 'end_date')
    def _check_date(self):
        for val in self:
            #if 'project_id' in self.env.context:
            if not val.project_id.date_start or not val.project_id.date:
                raise UserError(_('Project start date and end date should not be blank'))
            if val.project_id.date_start and val.start_date:
                if not (val.project_id.date_start <= val.start_date <= val.project_id.date):
                    raise ValidationError("Start date should be between project start date and End Date")
            if val.end_date:
                if not (val.project_id.date_start <= val.end_date <= val.project_id.date):
                    raise ValidationError("End date should be between project start date and End Date")
            domain = [
                ('start_date', '<=', val.end_date),
                ('end_date', '>=', val.start_date),
                ('project_id', '=', val.project_id.id),
                ('employee_id', '=', val.employee_id.id),
                ('id', '!=', val.id)
            ]
            res = self.search_count(domain)
            if res > 0:
                raise ValidationError(_('Same Consultant can not have 2 same date that overlaps on same day!'))

    @api.depends('project_id', 'employee_id', 'project_id.sale_line_employee_ids', 'percentage')
    def _compute_budgeted_hours(self):
        for val in self:
            if val.project_id and val.employee_id and val.percentage > 0:
                budgeted = 0
                for emp in val.project_id.sale_line_employee_ids:
                    if emp.employee_id.id == val.employee_id.id:
                        budgeted = emp.budgeted_qty
                val.budgeted_hours = (budgeted * val.percentage / 100)
            else:
                val.budgeted_hours = 0

    @api.depends('project_id', 'start_date', 'end_date', 'project_id.timesheet_ids', 'project_id.timesheet_ids.date', 'project_id.timesheet_ids.project_id',
                 'project_id.timesheet_ids.employee_id', 'project_id.timesheet_ids.start_datetime', 'project_id.timesheet_ids.end_datetime', 'project_id.timesheet_ids.unit_amount')
    def _compute_actual_calc(self):
        Timesheet = self.env['account.analytic.line']
        for val in self:
            if val.project_id and val.employee_id and val.start_date and val.end_date:
                domain = [
                    ('project_id','=',val.project_id.id),
                    ('employee_id', '=', val.employee_id.id),
                    ('start_datetime', '>=', val.start_date),
                    ('end_datetime', '<=', val.end_date)
                ]
                timesheets = Timesheet.search(domain)
                val.actual_hours = sum(timesheet.unit_amount for timesheet in timesheets)
                #budgeted = 0
                #for emp in val.project_id.sale_line_employee_ids:
                #    if emp.employee_id.id == val.employee_id.id:
                #        budgeted = emp.budgeted_qty
                res = sum(timesheet.unit_amount for timesheet in timesheets)
                val.actual_hours = res
                #budgeted_period = (budgeted * val.percentage / 100)
                if val.budgeted_hours > 0:
                    val.actual_percentage = (res/val.budgeted_hours) * 100
                else:
                    val.actual_percentage = 0
            else:
                val.actual_hours = 0
                val.actual_percentage = 0

    @api.onchange('start_date')
    def onchange_start_date(self):
        if self.start_date:
            self.end_date = self.start_date + relativedelta(days=6)

    @api.onchange('employee_id')
    def onchange_employee_id(self):
        res = {}
        emp_ids = [emp.employee_id.id for emp in self.project_id.sale_line_employee_ids]
        res['domain'] = {'employee_id': [('id', 'in', emp_ids)]}
        if 'project_id' in self.env.context:
            #if not self.env.context['project_id']:
                #raise UserError(_('Please save record before add a line'))
            if not emp_ids:
                raise UserError(_('Please add consultant at Invoicing tab before add a line'))
            res['domain'] = {'employee_id': [('id','in',emp_ids)]}
        return res

    """@api.onchange('employee_id', 'percentage')
    def onchange_employee_id(self):
        if self.employee_id and self.percentage > 0:
            cons = [emp.budgeted_qty if emp.employee_id.id == self.employee_id.id else 0 for emp in self.project_id.sale_line_employee_ids]
            budgeted = cons and cons[0] or 0
            if budgeted > 0:
                #record.employee_per = 100 - record.manager_per
                self.budgeted_hours = (budgeted * self.percentage / 100)
            else:
                self.budgeted_hours = 0"""

