import { APIRequestContext } from '@playwright/test';

export class ApiClient {
  private request: APIRequestContext;
  private baseURL: string;

  constructor(request: APIRequestContext, baseURL: string = 'http://localhost:8084') {
    this.request = request;
    this.baseURL = baseURL;
  }

  async login(username: string, password: string): Promise<{ token: string; userId: number }> {
    const response = await this.request.post(`${this.baseURL}/api/auth/login`, {
      data: {
        username,
        password,
      },
    });
    
    if (!response.ok()) {
      throw new Error(`Login failed: ${response.status()}`);
    }

    const data = await response.json();
    return {
      token: data.token,
      userId: data.userId,
    };
  }

  async logout(token: string): Promise<void> {
    await this.request.post(`${this.baseURL}/api/auth/logout`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });
  }

  async getUsers(token: string): Promise<any[]> {
    const response = await this.request.get(`${this.baseURL}/api/users`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    if (!response.ok()) {
      throw new Error(`Get users failed: ${response.status()}`);
    }

    return await response.json();
  }

  async createUser(token: string, userData: any): Promise<any> {
    const response = await this.request.post(`${this.baseURL}/api/users`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
      data: userData,
    });

    if (!response.ok()) {
      throw new Error(`Create user failed: ${response.status()}`);
    }

    return await response.json();
  }

  async updateUser(token: string, userId: number, userData: any): Promise<any> {
    const response = await this.request.put(`${this.baseURL}/api/users/${userId}`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
      data: userData,
    });

    if (!response.ok()) {
      throw new Error(`Update user failed: ${response.status()}`);
    }

    return await response.json();
  }

  async deleteUser(token: string, userId: number): Promise<void> {
    const response = await this.request.delete(`${this.baseURL}/api/users/${userId}`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    if (!response.ok()) {
      throw new Error(`Delete user failed: ${response.status()}`);
    }
  }

  async getRoles(token: string): Promise<any[]> {
    const response = await this.request.get(`${this.baseURL}/api/roles`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    if (!response.ok()) {
      throw new Error(`Get roles failed: ${response.status()}`);
    }

    return await response.json();
  }

  async createRole(token: string, roleData: any): Promise<any> {
    const response = await this.request.post(`${this.baseURL}/api/roles`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
      data: roleData,
    });

    if (!response.ok()) {
      throw new Error(`Create role failed: ${response.status()}`);
    }

    return await response.json();
  }

  async deleteRole(token: string, roleId: number): Promise<void> {
    const response = await this.request.delete(`${this.baseURL}/api/roles/${roleId}`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    if (!response.ok()) {
      throw new Error(`Delete role failed: ${response.status()}`);
    }
  }

  async getMenus(token: string): Promise<any[]> {
    const response = await this.request.get(`${this.baseURL}/api/menus`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
    });

    if (!response.ok()) {
      throw new Error(`Get menus failed: ${response.status()}`);
    }

    return await response.json();
  }

  async healthCheck(): Promise<{ status: string }> {
    const response = await this.request.get(`${this.baseURL}/actuator/health`);

    if (!response.ok()) {
      throw new Error(`Health check failed: ${response.status()}`);
    }

    return await response.json();
  }
}
