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

from odoo import models, fields, api, _
from odoo.exceptions import UserError
from datetime import datetime


class HouseAllowanceAdvance(models.Model):
    _name = 'house.allowance.advance'
    _description = 'House Allowance Advance'
    _rec_name = 'employee_id'

    from_hr_department = fields.Boolean()
    amount = fields.Float()
    date = fields.Date()
    start_date = fields.Date()
    duration = fields.Float(compute='get_Days_no')
    state = fields.Selection(
        [('draft', _('Draft')), ('send', _('Send')), ('hr_special_Approval', _('HR Specialist Approval')),
         ('hr_manager_approved', _('HR Manager Approved')),
         ('financial_manager', _('Financial Manager Approval')),
         ('approve_manager', _('Approve Manager')), ('refused', _('Refused'))],
        default="draft")
    # relational fields
    job_id = fields.Many2one(related='employee_id.job_id', readonly=True)
    department_id = fields.Many2one(related='employee_id.department_id', readonly=True)
    contract_id = fields.Many2one(comodel_name='hr.contract')
    account_move_id = fields.Many2one(comodel_name='hr.account.moves', string='Move')
    house_allowance_advance_line_ids = fields.One2many('house.allowance.advance.line', 'amount_id')
    employee_id = fields.Many2one('hr.employee', 'Employee Id', default=lambda item: item.get_user_id())

    @api.depends('start_date', 'date')
    def get_Days_no(self):
        for item in self:
            if item.start_date and item.date:
                start_date_value = datetime.strptime(str(item.start_date), "%Y-%m-%d")
                end_date = datetime.strptime(str(item.date), "%Y-%m-%d")
                if start_date_value > end_date:
                    raise UserError(_('End Date must be greater than Start Date'))
                elif start_date_value < end_date:
                    days = (end_date - start_date_value).days
                    item.duration = days
                else:
                    item.duration = 0.0
            else:
                item.duration = 0.0

    def draft_state(self):
        self.state = "draft"

    def send(self):
        self.state = "send"

    def hr_special_Approval(self):
        self.state = "hr_special_Approval"

    def financial_manager(self):
        self.state = "financial_manager"

    def hr_manager_approved(self):
        self.state = "hr_manager_approved"

    def approve_manager(self):
        self.state = "approve_manager"

    def refused(self):
        self.state = "refused"

    def get_user_id(self):
        employee_id = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1)
        if employee_id:
            return employee_id.id
        else:
            return False

    def unlink(self):
        for i in self:
            if i.state != 'draft':
                raise UserError(_('You can not delete record in state not in draft'))
        return super(HouseAllowanceAdvance, self).unlink()


class HouseAllowanceAdvanceLine(models.Model):
    _name = 'house.allowance.advance.line'
    _description = 'House Allowance Advance Line'

    date = fields.Date()

    # relational fields
    amount_id = fields.Many2one(comodel_name='house.allowance.advance', string='Amount')
