# -*- coding: utf-8 -*-
from odoo import fields, models


class FamilyLawProceeding(models.Model):
    _name = "familylaw.proceeding"
    _description = "Proceeding"
    _inherit = ["mail.thread", "mail.activity.mixin"]
    _order = "date_opened desc"

    case_id = fields.Many2one(
        "familylaw.case",
        required=True,
        ondelete="cascade",
        index=True,
        tracking=True,
    )
    name = fields.Char(
        string="Proceeding",
        required=True,
        tracking=True,
    )
    proceeding_type = fields.Selection(
        selection=[
            ("original", "Original Action"),
            ("modification", "Modification"),
            ("enforcement", "Enforcement / Contempt"),
            ("appeal", "Appeal"),
            ("other", "Other"),
        ],
        string="Type",
        required=True,
        default="original",
        tracking=True,
    )
    state = fields.Selection(
        selection=[
            ("open", "Open"),
            ("closed", "Closed"),
        ],
        default="open",
        required=True,
        tracking=True,
    )
    proceeding_number = fields.Char(
        string="Court-Assigned Number",
        help="If the court assigns a distinct number for this proceeding.",
    )
    date_opened = fields.Date(
        string="Date Opened",
        default=fields.Date.context_today,
    )
    date_closed = fields.Date(string="Date Closed")
    notes = fields.Text()

    # Documents and deadlines attach to the PROCEEDING (locked decision).
    document_ids = fields.One2many(
        "familylaw.document", "proceeding_id", string="Documents",
    )
    deadline_ids = fields.One2many(
        "familylaw.deadline", "proceeding_id", string="Deadlines",
    )
    disclosure_item_ids = fields.One2many(
        "familylaw.disclosure.item", "proceeding_id", string="Disclosure Items",
    )
    affidavit_ids = fields.One2many(
        "familylaw.financial.affidavit", "proceeding_id", string="Financial Affidavits",
    )
    discovery_ids = fields.One2many(
        "familylaw.discovery.request", "proceeding_id", string="Discovery",
    )

    def action_seed_disclosure_items(self):
        """Seed the standard Rule 12.285 mandatory disclosure checklist.
        VERIFY the current rule's required-document list before relying on it."""
        Item = self.env["familylaw.disclosure.item"]
        standard = [
            "Federal income tax returns (last 3 years)",
            "IRS forms W-2, 1099, K-1 (last year)",
            "Pay stubs (last 3 months)",
            "Loan applications / financial statements (last 12 months)",
            "Bank / checking / savings statements (last 3 months)",
            "Deeds, leases, and mortgages on real property",
            "Credit card and other liability statements (last 3 months)",
            "Retirement / pension / profit-sharing statements (last 12 months)",
        ]
        for proc in self:
            existing = set(proc.disclosure_item_ids.mapped("name"))
            for seq, label in enumerate(standard, start=1):
                if label in existing:
                    continue
                Item.create({
                    "proceeding_id": proc.id,
                    "name": label,
                    "sequence": seq * 10,
                    "is_mandatory": True,
                })
            proc.message_post(body="Mandatory disclosure checklist seeded (Rule 12.285).")
        return True

    def action_seed_standard_deadlines(self):
        """Create the common procedural deadlines for this proceeding, dated from
        today. The day-counts come from familylaw.deadline.STANDARD_OFFSETS and MUST
        be confirmed against the current rule — see that module's date-math note."""
        Deadline = self.env["familylaw.deadline"]
        today = fields.Date.context_today(self)
        labels = {
            "answer": "Answer / Response due",
            "disclosure": "Mandatory Disclosure due (Rule 12.285)",
            "discovery": "Discovery responses due",
        }
        for proc in self:
            existing = set(proc.deadline_ids.mapped("deadline_type"))
            for dtype, label in labels.items():
                if dtype in existing:
                    continue
                Deadline.create({
                    "proceeding_id": proc.id,
                    "name": label,
                    "deadline_type": dtype,
                    "trigger_date": today,
                })
            proc.message_post(body="Standard deadlines seeded.")
        return True

    def action_close_proceeding(self):
        for proc in self:
            proc.state = "closed"
            proc.date_closed = fields.Date.context_today(self)
            proc.message_post(body="Proceeding closed.")

    def action_reopen_proceeding(self):
        for proc in self:
            proc.state = "open"
            proc.date_closed = False
            proc.message_post(body="Proceeding reopened.")
