/**
 * [MOCK-009] 用户模块DataSource
 * AI注意: 这是Mock实现，不是真实业务逻辑
 * 仅在USE_MOCK=true时启用
 */

import { User } from '../types/user';

// 用户数据接口
export interface UserDataSource {
  list(params?: {
    page?: number;
    pageSize?: number;
    role?: string;
    status?: string;
  }): Promise<{ data: User[]; total: number }>;
  
  get(id: string): Promise<User | null>;
  
  create(user: Omit<User, 'id' | 'createdAt' | 'updatedAt'>): Promise<User>;
  
  update(id: string, user: Partial<User>): Promise<User | null>;
  
  delete(id: string): Promise<boolean>;
  
  updateStatus(id: string, status: string): Promise<User | null>;
  
  search(keyword: string): Promise<User[]>;
  
  getByRole(role: string): Promise<User[]>;
  
  getProfile(userId: string): Promise<{
    user: User;
    statistics: {
      totalOrders: number;
      totalSpent: number;
      lastLogin: Date;
    };
  } | null>;
}

// 用户DataSource实现
export class UserDataSourceImpl implements UserDataSource {
  async list(params?: {
    page?: number;
    pageSize?: number;
    role?: string;
    status?: string;
  }): Promise<{ data: User[]; total: number }> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const mockUsers: User[] = [
      {
        id: 'cust_1',
        name: '张三',
        email: 'zhangsan@example.com',
        phone: '13800138001',
        role: 'customer',
        status: 'active',
        createdAt: new Date('2026-03-01'),
        updatedAt: new Date('2026-03-10'),
      },
      {
        id: 'cust_2',
        name: '李四',
        email: 'lisi@example.com',
        phone: '13900139002',
        role: 'customer',
        status: 'active',
        createdAt: new Date('2026-03-02'),
        updatedAt: new Date('2026-03-11'),
      },
      {
        id: 'admin_1',
        name: '管理员',
        email: 'admin@example.com',
        phone: '13700137003',
        role: 'admin',
        status: 'active',
        createdAt: new Date('2026-02-01'),
        updatedAt: new Date('2026-03-01'),
      },
    ];

    let filteredUsers = mockUsers;
    
    if (params?.role) {
      filteredUsers = filteredUsers.filter(u => u.role === params.role);
    }
    
    if (params?.status) {
      filteredUsers = filteredUsers.filter(u => u.status === params.status);
    }

    const page = params?.page || 1;
    const pageSize = params?.pageSize || 10;
    const start = (page - 1) * pageSize;
    const end = start + pageSize;
    const paginatedUsers = filteredUsers.slice(start, end);

    return {
      data: paginatedUsers,
      total: filteredUsers.length,
    };
  }

  async get(id: string): Promise<User | null> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const mockUsers: User[] = [
      {
        id: 'cust_1',
        name: '张三',
        email: 'zhangsan@example.com',
        phone: '13800138001',
        role: 'customer',
        status: 'active',
        createdAt: new Date('2026-03-01'),
        updatedAt: new Date('2026-03-10'),
      },
      {
        id: 'cust_2',
        name: '李四',
        email: 'lisi@example.com',
        phone: '13900139002',
        role: 'customer',
        status: 'active',
        createdAt: new Date('2026-03-02'),
        updatedAt: new Date('2026-03-11'),
      },
      {
        id: 'admin_1',
        name: '管理员',
        email: 'admin@example.com',
        phone: '13700137003',
        role: 'admin',
        status: 'active',
        createdAt: new Date('2026-02-01'),
        updatedAt: new Date('2026-03-01'),
      },
    ];

    const user = mockUsers.find(u => u.id === id);
    return user || null;
  }

  async create(user: Omit<User, 'id' | 'createdAt' | 'updatedAt'>): Promise<User> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const newUser: User = {
      ...user,
      id: `user_${Date.now()}`,
      createdAt: new Date(),
      updatedAt: new Date(),
    };

    return newUser;
  }

  async update(id: string, user: Partial<User>): Promise<User | null> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const existingUser = await this.get(id);
    if (!existingUser) {
      return null;
    }

    const updatedUser: User = {
      ...existingUser,
      ...user,
      updatedAt: new Date(),
    };

    return updatedUser;
  }

  async delete(id: string): Promise<boolean> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    return true;
  }

  async updateStatus(id: string, status: string): Promise<User | null> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const existingUser = await this.get(id);
    if (!existingUser) {
      return null;
    }

    const updatedUser: User = {
      ...existingUser,
      status: status as any,
      updatedAt: new Date(),
    };

    return updatedUser;
  }

  async search(keyword: string): Promise<User[]> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const mockUsers: User[] = [
      {
        id: 'cust_1',
        name: '张三',
        email: 'zhangsan@example.com',
        phone: '13800138001',
        role: 'customer',
        status: 'active',
        createdAt: new Date('2026-03-01'),
        updatedAt: new Date('2026-03-10'),
      },
      {
        id: 'cust_2',
        name: '李四',
        email: 'lisi@example.com',
        phone: '13900139002',
        role: 'customer',
        status: 'active',
        createdAt: new Date('2026-03-02'),
        updatedAt: new Date('2026-03-11'),
      },
      {
        id: 'admin_1',
        name: '管理员',
        email: 'admin@example.com',
        phone: '13700137003',
        role: 'admin',
        status: 'active',
        createdAt: new Date('2026-02-01'),
        updatedAt: new Date('2026-03-01'),
      },
    ];

    return mockUsers.filter(u => 
      u.name.toLowerCase().includes(keyword.toLowerCase()) ||
      u.email.toLowerCase().includes(keyword.toLowerCase()) ||
      u.phone.includes(keyword)
    );
  }

  async getByRole(role: string): Promise<User[]> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const mockUsers: User[] = [
      {
        id: 'cust_1',
        name: '张三',
        email: 'zhangsan@example.com',
        phone: '13800138001',
        role: 'customer',
        status: 'active',
        createdAt: new Date('2026-03-01'),
        updatedAt: new Date('2026-03-10'),
      },
      {
        id: 'cust_2',
        name: '李四',
        email: 'lisi@example.com',
        phone: '13900139002',
        role: 'customer',
        status: 'active',
        createdAt: new Date('2026-03-02'),
        updatedAt: new Date('2026-03-11'),
      },
      {
        id: 'admin_1',
        name: '管理员',
        email: 'admin@example.com',
        phone: '13700137003',
        role: 'admin',
        status: 'active',
        createdAt: new Date('2026-02-01'),
        updatedAt: new Date('2026-03-01'),
      },
    ];

    return mockUsers.filter(u => u.role === role);
  }

  async getProfile(userId: string): Promise<{
    user: User;
    statistics: {
      totalOrders: number;
      totalSpent: number;
      lastLogin: Date;
    };
  } | null> {
    // 这里应该调用实际的API
    // 暂时返回Mock数据
    const user = await this.get(userId);
    if (!user) {
      return null;
    }

    const statistics = {
      totalOrders: Math.floor(Math.random() * 100) + 1,
      totalSpent: Math.random() * 10000 + 1000,
      lastLogin: new Date(),
    };

    return {
      user,
      statistics,
    };
  }
}

// 导出DataSource实例
export const userDataSource = new UserDataSourceImpl();
