from django.test import TestCase
from ..services.user_service import UserService
from ..models import BaseInfo, ExtendField, DetailInfo
from ..utils.j_ulid_field import JULIDField

class TestUserEdit(TestCase):
    def setUp(self):
        # 初始化测试数据
        self.user = BaseInfo.objects.create(
            username="testuser",
            fullname="Test User",
            nickname="testnick",
            email="test@example.com",
            phone="1234567890",
            user_info="{}",
            privacies="{}",
            is_using=True,
            uuid=JULIDField.get_u12()
        )

        # 初始化ExtendField数据
        ExtendField.objects.create(
            field_index="field_1",
            field="school_id",
            type="int",
            description="学校id"
        )
        ExtendField.objects.create(
            field_index="field_2",
            field="grade",
            type="int",
            description="年级"
        )

    def test_user_edit_base_info(self):
        # 测试更新基础信息
        params = {
            "fullname": "Updated User",
            "nickname": "updatednick",
            "email": "updated@example.com",
            "phone": "0987654321",
            "is_using": False
        }
        result, err = UserService.user_edit(params=params, user_id=self.user.id)
        self.assertEqual(err, None)
        self.assertEqual(result["fullname"], "Updated User")
        self.assertEqual(result["nickname"], "updatednick")
        self.assertEqual(result["email"], "updated@example.com")
        self.assertEqual(result["phone"], "0987654321")
        self.assertEqual(result["is_using"], False)

    def test_user_edit_detail_info(self):
        # 测试更新详细信息
        params = {
            "school_id": 123,
            "grade": 10
        }
        result, err = UserService.user_edit(params=params, user_id=self.user.id)
        self.assertEqual(err, None)

        # 验证详细信息
        detail_info = DetailInfo.objects.filter(user_id=self.user.id).first()
        self.assertIsNotNone(detail_info)
        self.assertEqual(detail_info.field_1, '123')
        self.assertEqual(detail_info.field_2, '10')

    def test_user_edit_with_invalid_user_id(self):
        # 测试使用无效的用户ID进行更新
        params = {
            "fullname": "Invalid User"
        }
        result, err = UserService.user_edit(params=params, user_id=9999)  # 假设9999是一个不存在的用户ID
        self.assertIsNotNone(err)
        self.assertEqual(err, "用户不存在")

    def test_user_edit_with_duplicate_email(self):
        # 测试更新时使用已被其他用户绑定的邮箱
        another_user = BaseInfo.objects.create(
            username="anotheruser",
            fullname="Another User",
            email="another@example.com",
            phone="1111111111",
            uuid=JULIDField.get_u12()
        )
        params = {
            "email": "another@example.com"
        }
        result, err = UserService.user_edit(params=params, user_id=self.user.id)
        self.assertIsNotNone(err)
        self.assertEqual(err, "当前邮箱被绑定")

    def test_user_edit_with_user_uuid(self):
        # 测试通过 user_uuid 更新用户信息
        params = {
            "fullname": "Updated User via UUID",
            "nickname": "updatednick_via_uuid",
            "email": "updated_via_uuid@example.com",
            "phone": "0987654321",
            "is_using": False
        }
        result, err = UserService.user_edit(params=params, user_uuid=self.user.uuid)
        self.assertEqual(err, None)
        self.assertEqual(result["fullname"], "Updated User via UUID")
        self.assertEqual(result["nickname"], "updatednick_via_uuid")
        self.assertEqual(result["email"], "updated_via_uuid@example.com")
        self.assertEqual(result["phone"], "0987654321")
        self.assertEqual(result["is_using"], False)