# -*- coding: utf-8 -*-
from dateutil.relativedelta import relativedelta
from odoo import models, fields, api, _
from odoo.exceptions import UserError
from odoo.tools import date_utils


class BenefitExpenseLine(models.Model):
    _name = 'benefit.expense.line'
    _description = 'Benefit Expense Line'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _rec_name = 'family_id'

    confirm_expense_id = fields.Many2one(comodel_name='confirm.benefit.expense', string='Confirm Benefit Expense',
                                         ondelete='cascade')
    return_confirm_id = fields.Many2one('confirm.benefit.expense', string="Return Confirm", ondelete='cascade', )
    family_id = fields.Many2one(comodel_name='grant.benefit', string='Family', required=True)
    branch_id = fields.Many2one(comodel_name='branch.settings', string='Branch')
    family_category_id = fields.Many2one(comodel_name='benefit.category', string='Family Category')
    meal_card = fields.Boolean(string="Meal Card")
    benefit_member_count = fields.Integer(string="Members count")
    family_monthly_income = fields.Float(string="Family Monthly Income")
    family_monthly_meals = fields.Float(string="Family Monthly Meals")
    family_monthly_clotting = fields.Float(string="Family Monthly Clotting")
    family_monthly_othaime = fields.Float(string="Othaim Total Monthly")
    total_family_expenses = fields.Float(string="Total Family Expenses", compute='_compute_total_family_expenses')
    start_date = fields.Date(string='Start Date', )
    end_date = fields.Date(string='End Date', )
    payment_order_id = fields.Many2one('payment.orders', string='Payment Order', ondelete="set null", copy=False)
    return_reason_id = fields.Many2one("return.reason", string="Return Reason")
    is_return = fields.Boolean(string="Is Returned?", default=False)
    state = fields.Selection([
        ('draft', 'Draft'),
        ('waiting_processing', 'Waiting Processing'),
        ('processing_done', 'Processing Done'),
    ], string="Status", default='draft', tracking=True)
    researcher_id = fields.Many2one("committees.line", string="Researcher", tracking=True, copy=False)

    @api.depends('family_monthly_income', 'family_monthly_clotting', 'family_monthly_meals')
    def _compute_total_family_expenses(self):
        for rec in self:
            rec.total_family_expenses = rec.family_monthly_income + rec.family_monthly_meals + rec.family_monthly_clotting

    def action_return_bank(self):
        self.ensure_one()
        return {
            'name': _("Bank Return"),
            'type': 'ir.actions.act_window',
            'res_model': 'return.reason.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'default_line_id': self.id,
                'default_line_model': 'benefit.expense.line',
            }
        }

    def action_process_return(self):
        self.ensure_one()
        if self.state != 'waiting_processing':
            raise UserError(_("You can only process when status is 'Waiting for Processing'."))

        self.message_post(
            body=_('<b>Processing Completed</b><br/><b>Processed By:</b> %s') % self.env.user.name,
            message_type='notification',
        )

        self.state = 'processing_done'
