import { apiService } from "./api";
import {
  ApiResponse,
  User,
  CreateUserRequest,
  UpdateUserRequest,
  PaginationParams,
} from "./types";

export const userService = {
  getUsers: async (params?: PaginationParams): Promise<ApiResponse<User[]>> => {
    try {
      return await apiService.get<ApiResponse<User[]>>("/users", params);
    } catch (error) {
      return {
        success: false,
        data: [],
        message: "خطا در دریافت کاربران",
      };
    }
  },

  getUser: async (id: string): Promise<ApiResponse<User>> => {
    try {
      return await apiService.get<ApiResponse<User>>(`/users/${id}`);
    } catch (error) {
      return {
        success: false,
        data: {} as User,
        message: "خطا در دریافت کاربر",
      };
    }
  },

  createUser: async (
    userData: CreateUserRequest
  ): Promise<ApiResponse<User>> => {
    try {
      return await apiService.post<ApiResponse<User>>("/users", userData);
    } catch (error) {
      return {
        success: false,
        data: {} as User,
        message: "خطا در ایجاد کاربر",
      };
    }
  },

  updateUser: async (
    id: string,
    userData: UpdateUserRequest
  ): Promise<ApiResponse<User>> => {
    try {
      return await apiService.put<ApiResponse<User>>(`/users/${id}`, userData);
    } catch (error) {
      return {
        success: false,
        data: {} as User,
        message: "خطا در ویرایش کاربر",
      };
    }
  },

  deleteUser: async (id: string): Promise<ApiResponse<void>> => {
    try {
      return await apiService.delete<ApiResponse<void>>(`/users/${id}`);
    } catch (error) {
      return {
        success: false,
        data: undefined,
        message: "خطا در حذف کاربر",
      };
    }
  },

  getMockUsers: (): User[] => [
    {
      id: "1",
      name: "علی احمدی",
      email: "ali@example.com",
      phone: "09123456789",
      role: "user",
      status: "active",
      permissions: [10, 15],
      createdAt: "2024-01-15T10:30:00Z",
      updatedAt: "2024-01-15T10:30:00Z",
    },
    {
      id: "2",
      name: "فاطمه حسینی",
      email: "fateme@example.com",
      phone: "09123456789",
      role: "admin",
      status: "active",
      permissions: [10, 15, 20, 22, 25, 30],
      createdAt: "2024-01-14T10:30:00Z",
      updatedAt: "2024-01-14T10:30:00Z",
    },
    {
      id: "3",
      name: "محمد رضایی",
      email: "mohammad@example.com",
      phone: "09123456789",
      role: "user",
      status: "inactive",
      permissions: [10],
      createdAt: "2024-01-13T10:30:00Z",
      updatedAt: "2024-01-13T10:30:00Z",
    },
    {
      id: "4",
      name: "زهرا کریمی",
      email: "zahra@example.com",
      phone: "09123456789",
      role: "user",
      status: "active",
      permissions: [10, 15],
      createdAt: "2024-01-12T10:30:00Z",
      updatedAt: "2024-01-12T10:30:00Z",
    },
    {
      id: "5",
      name: "حسن نوری",
      email: "hassan@example.com",
      phone: "09123456789",
      role: "admin",
      status: "active",
      permissions: [10, 15, 20, 22, 25, 30],
      createdAt: "2024-01-11T10:30:00Z",
      updatedAt: "2024-01-11T10:30:00Z",
    },
  ],
};
