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

from odoo import api, fields, models, _


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

    maintenance_ids = fields.One2many('project.maintenance.details', 'project_id', string='Maintenance', copy=False)


class ProjectMainenenceDetails(models.Model):
    _name = 'project.maintenance.details'
    _description = "project Maintenance Details"

    @api.depends('line_ids')
    def _compute_amount(self):
        for val in self:
            total_cost = 0
            for line in val.line_ids:
                if line.cost:
                    total_cost += line.cost
            val.total_amount = total_cost

    project_id = fields.Many2one('project.project', 'Project', required=True, ondelete='cascade')
    maintenance_type_id = fields.Many2one('project.maintenance.type', 'Maintenance Type', required=True, ondelete="restrict")
    currency_id = fields.Many2one('res.currency', string='Currency', readonly=True, default=lambda self: self.env.company.currency_id)
    total_amount = fields.Monetary("Total Amount",  compute='_compute_amount', store=True, currency_field='currency_id')
    line_ids = fields.One2many('project.maintenance.lines', 'details_id', string='Maintenance Details', copy=False)

class ProjectMainenencelines(models.Model):
    _name = 'project.maintenance.lines'
    _description = "project Maintenance Lines"

    details_id = fields.Many2one('project.maintenance.details', 'Details', required=True, ondelete='cascade')
    maintenance_id = fields.Many2one('project.maintenance', 'Maintenance', required=True, ondelete="restrict")
    currency_id = fields.Many2one('res.currency', string='Currency', readonly=True, default=lambda self: self.env.company.currency_id)
    cost = fields.Monetary("Cost", currency_field='currency_id')
