import { describe, it, expect } from 'vitest';
import { CreatePlatformSchema, UpdatePlatformSchema } from '../../src/schemas/platform.schema';

describe('平台Schema验证测试', () => {
  describe('CreatePlatformSchema', () => {
    it('应该验证空字符串转换为undefined', () => {
      const data = {
        platformName: '测试平台',
        contactPerson: '张三',
        contactPhone: '13800138000',
        contactEmail: '' // 空字符串
      };

      const result = CreatePlatformSchema.safeParse(data);

      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.contactEmail).toBeUndefined();
      }
    });

    it('应该验证undefined通过验证（可选字段）', () => {
      const data = {
        platformName: '测试平台',
        contactPerson: '张三',
        contactPhone: '13800138000'
        // contactEmail字段不存在，应该是undefined
      };

      const result = CreatePlatformSchema.safeParse(data);

      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.contactEmail).toBeUndefined();
      }
    });

    it('应该验证有效邮箱格式通过验证', () => {
      const data = {
        platformName: '测试平台',
        contactPerson: '张三',
        contactPhone: '13800138000',
        contactEmail: 'zhangsan@example.com'
      };

      const result = CreatePlatformSchema.safeParse(data);

      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.contactEmail).toBe('zhangsan@example.com');
      }
    });

    it('应该验证无效邮箱格式显示中文错误提示', () => {
      const data = {
        platformName: '测试平台',
        contactPerson: '张三',
        contactPhone: '13800138000',
        contactEmail: '无效邮箱'
      };

      const result = CreatePlatformSchema.safeParse(data);

      expect(result.success).toBe(false);
      if (!result.success) {
        // Zod错误格式：result.error.issues[0].message
        const errorMessage = result.error.issues[0].message;
        expect(errorMessage).toBe('请输入有效的邮箱地址');
      }
    });

    it('应该验证超长邮箱地址显示中文长度错误提示', () => {
      const longEmail = 'a'.repeat(100) + '@example.com'; // 超过100个字符
      const data = {
        platformName: '测试平台',
        contactPerson: '张三',
        contactPhone: '13800138000',
        contactEmail: longEmail
      };

      const result = CreatePlatformSchema.safeParse(data);

      expect(result.success).toBe(false);
      if (!result.success) {
        const errorMessage = result.error.issues[0].message;
        expect(errorMessage).toBe('邮箱地址不能超过100个字符');
      }
    });

    it('应该验证平台名称必填', () => {
      const data = {
        // platformName字段缺失
        contactPerson: '张三',
        contactPhone: '13800138000'
      };

      const result = CreatePlatformSchema.safeParse(data);

      expect(result.success).toBe(false);
      if (!result.success) {
        const errorMessage = result.error.issues[0].message;
        // Zod的错误消息可能是"Required"或其他，我们检查它是否包含错误
        expect(errorMessage).toBeDefined();
      }
    });

    it('应该验证平台名称不能为空字符串', () => {
      const data = {
        platformName: '', // 空字符串
        contactPerson: '张三',
        contactPhone: '13800138000'
      };

      const result = CreatePlatformSchema.safeParse(data);

      expect(result.success).toBe(false);
      if (!result.success) {
        const errorMessage = result.error.issues[0].message;
        expect(errorMessage).toBe('平台名称不能为空');
      }
    });

    it('应该验证平台名称超长显示中文错误提示', () => {
      const longName = 'a'.repeat(101); // 超过100个字符
      const data = {
        platformName: longName,
        contactPerson: '张三',
        contactPhone: '13800138000'
      };

      const result = CreatePlatformSchema.safeParse(data);

      expect(result.success).toBe(false);
      if (!result.success) {
        const errorMessage = result.error.issues[0].message;
        expect(errorMessage).toBe('平台名称不能超过100个字符');
      }
    });
  });

  describe('UpdatePlatformSchema', () => {
    it('应该验证空字符串转换为undefined', () => {
      const data = {
        id: 1,
        platformName: '更新后的平台',
        contactEmail: '' // 空字符串
      };

      const result = UpdatePlatformSchema.safeParse(data);

      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.contactEmail).toBeUndefined();
      }
    });

    it('应该验证undefined通过验证（可选字段）', () => {
      const data = {
        id: 1,
        platformName: '更新后的平台'
        // contactEmail字段不存在，应该是undefined
      };

      const result = UpdatePlatformSchema.safeParse(data);

      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.contactEmail).toBeUndefined();
      }
    });

    it('应该验证有效邮箱格式通过验证', () => {
      const data = {
        id: 1,
        platformName: '更新后的平台',
        contactEmail: 'lisi@example.com'
      };

      const result = UpdatePlatformSchema.safeParse(data);

      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.contactEmail).toBe('lisi@example.com');
      }
    });

    it('应该验证无效邮箱格式显示中文错误提示', () => {
      const data = {
        id: 1,
        platformName: '更新后的平台',
        contactEmail: '无效邮箱'
      };

      const result = UpdatePlatformSchema.safeParse(data);

      expect(result.success).toBe(false);
      if (!result.success) {
        const errorMessage = result.error.issues[0].message;
        expect(errorMessage).toBe('请输入有效的邮箱地址');
      }
    });

    it('应该验证超长邮箱地址显示中文长度错误提示', () => {
      const longEmail = 'a'.repeat(100) + '@example.com'; // 超过100个字符
      const data = {
        id: 1,
        platformName: '更新后的平台',
        contactEmail: longEmail
      };

      const result = UpdatePlatformSchema.safeParse(data);

      expect(result.success).toBe(false);
      if (!result.success) {
        const errorMessage = result.error.issues[0].message;
        expect(errorMessage).toBe('邮箱地址不能超过100个字符');
      }
    });

    it('应该验证ID必填', () => {
      const data = {
        // id字段缺失
        platformName: '更新后的平台'
      };

      const result = UpdatePlatformSchema.safeParse(data);

      expect(result.success).toBe(false);
      if (!result.success) {
        const errorMessage = result.error.issues[0].message;
        expect(errorMessage).toContain('number');
      }
    });

    it('应该验证平台名称超长显示错误', () => {
      const longName = 'a'.repeat(101); // 超过100个字符
      const data = {
        id: 1,
        platformName: longName
      };

      const result = UpdatePlatformSchema.safeParse(data);

      expect(result.success).toBe(false);
      if (!result.success) {
        const errorMessage = result.error.issues[0].message;
        expect(errorMessage).toContain('100');
      }
    });
  });
});