from odoo.tests import common, tagged
from odoo.exceptions import ValidationError


@tagged('post_install', '-at_install')
class TestCustomerValidation(common.TransactionCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.user_no_group = cls.env['res.users'].sudo().create({
            'name': 'User No Group', 'login': 'no_group', 'email': 'no@group.com'
        })
        cls.user_with_group = cls.env['res.users'].sudo().create({
            'name': 'User With Group', 'login': 'with_group', 'email': 'with@group.com'
        })

        cls.group = cls.env.ref('abs_customer_validation.group_activate_customer_validation')
        cls.group.sudo().users |= cls.user_with_group

        cls.existing_partner1 = cls.env['res.partner'].sudo().create({
            'name': 'Existing Partner 1', 'phone': '+1234567890', 'mobile': '+1987654321',
            'email': 'test@example.com', 'website': 'http://test.com'
        })
        cls.existing_partner2 = cls.env['res.partner'].sudo().create({
            'name': 'Existing Partner 2', 'phone': '+1234567890'
        })

    def test_onchange_name_no_group(self):
        partner = self.env['res.partner'].with_user(self.user_no_group.id).new({'name': 'Existing Partner 1'})
        partner.onchange_name()  # لا خطأ

    def test_onchange_name_with_group(self):
        partner = self.env['res.partner'].with_user(self.user_with_group.id).new({'name': 'Existing Partner 1'})
        with self.assertRaises(ValidationError):
            partner.onchange_name()

    def test_onchange_phone_no_group(self):
        partner = self.env['res.partner'].with_user(self.user_no_group.id).new({'phone': '+1234567890'})
        partner.onchange_phonenumber()  # لا خطأ

    def test_onchange_phone_with_group(self):
        partner = self.env['res.partner'].with_user(self.user_with_group.id).new({'phone': '+1234567890'})
        with self.assertRaises(ValidationError) as cm:
            partner.onchange_phonenumber()
        self.assertIn('Existing Partner 1', str(cm.exception))
        self.assertIn('Existing Partner 2', str(cm.exception))

    def test_onchange_phone_unique(self):
        partner = self.env['res.partner'].with_user(self.user_with_group.id).new({'phone': '+9999999999'})
        partner.onchange_phonenumber()  # يمر

    def test_onchange_mobile_with_group(self):
        partner = self.env['res.partner'].with_user(self.user_with_group.id).new({'mobile': '+1987654321'})
        with self.assertRaises(ValidationError):
            partner.onchange_mobilenumber()

    def test_onchange_email_with_group(self):
        partner = self.env['res.partner'].with_user(self.user_with_group.id).new({'email': 'test@example.com'})
        with self.assertRaises(ValidationError):
            partner.onchange_email()

    def test_onchange_website_with_group(self):
        partner = self.env['res.partner'].with_user(self.user_with_group.id).new({'website': 'test.com'})
        with self.assertRaises(ValidationError):
            partner.onchange_website()

    def test_empty_fields(self):
        partner = self.env['res.partner'].with_user(self.user_with_group.id).new({})
        partner.onchange_name()
        partner.onchange_phonenumber()
        partner.onchange_email()  # كلها تمر
