# -*- coding: utf-8 -*-
"""
Tests for mklab_dms_document — DMS document lifecycle, PDF creation,
and template rendering.

Validates: Requirements 18.1, 18.2, 18.3, 18.4
"""
from odoo.tests.common import TransactionCase
from odoo.exceptions import ValidationError, UserError


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _make_partner(env):
    """Create a minimal company partner."""
    return env['res.partner'].create({
        'name': 'Test DMS Partner',
        'is_company': True,
    })


def _make_document(env, partner, state='draft', text=None):
    """Create a minimal dms.document record."""
    vals = {
        'type_document': 'incoming',
        'partner_id': partner.id,
        'name': 'TEST-DOC-001',
        'state': state,
    }
    if text is not None:
        vals['text'] = text
    return env['dms.document'].create(vals)


# ---------------------------------------------------------------------------
# TestDmsDocument
# ---------------------------------------------------------------------------

class TestDmsDocument(TransactionCase):
    """
    Tests for dms.document: unlink restrictions, create_pdf validation,
    and render_template output.

    Validates: Requirements 18.1, 18.2, 18.3, 18.4
    """

    def setUp(self):
        super().setUp()
        self.partner = _make_partner(self.env)

    # ------------------------------------------------------------------
    # Requirement 18.1 — unlink of a 'done' document raises ValidationError
    # ------------------------------------------------------------------

    def test_unlink_done_raises(self):
        """
        Req 18.1 — deleting a document in state 'done' must raise
        ValidationError.
        """
        doc = _make_document(self.env, self.partner, state='done')
        with self.assertRaises(ValidationError):
            doc.unlink()

    # ------------------------------------------------------------------
    # Requirement 18.2 — unlink of a draft document succeeds
    # ------------------------------------------------------------------

    def test_unlink_draft_ok(self):
        """
        Req 18.2 — deleting a document in state 'draft' must succeed
        without raising any exception.
        """
        doc = _make_document(self.env, self.partner, state='draft')
        # Should not raise
        doc.unlink()

    # ------------------------------------------------------------------
    # Requirement 18.3 — create_pdf without directory raises UserError
    # ------------------------------------------------------------------

    def test_create_pdf_no_directory_raises(self):
        """
        Req 18.3 — calling create_pdf on a document with no directory_id
        set must raise UserError.
        """
        doc = _make_document(self.env, self.partner, state='draft')
        # Ensure no directory is set
        doc.directory_id = False
        with self.assertRaises(UserError):
            doc.create_pdf()

    # ------------------------------------------------------------------
    # Requirement 18.4 — render_template with a simple template returns
    # a non-empty string
    # ------------------------------------------------------------------

    def test_render_template_basic(self):
        """
        Req 18.4 — render_template on a document with a simple Mako/Jinja2
        template must return a non-empty string.
        """
        doc = _make_document(
            self.env,
            self.partner,
            state='draft',
            text='<p>Hello World</p>',
        )
        result = doc.render_template()
        self.assertTrue(
            result and len(result) > 0,
            "render_template should return a non-empty string for a simple template",
        )
