import { test, expect } from '@playwright/test';
import { createTestUser, registerUser } from '../fixtures';

test.describe('POST /auth/login', () => {
  test('logs in with valid credentials and returns token pair', async ({ request }) => {
    const user = createTestUser();
    await registerUser(request, user);

    const res = await request.post('auth/login', {
      data: { phone: user.phone, password: user.password },
    });

    expect(res.status()).toBe(201);
    const body = await res.json();
    expect(body).toHaveProperty('accessToken');
    expect(body).toHaveProperty('refreshToken');
  });

  test('rejects login with wrong password', async ({ request }) => {
    const user = createTestUser();
    await registerUser(request, user);

    const res = await request.post('auth/login', {
      data: { phone: user.phone, password: 'WrongPassword!1' },
    });

    expect(res.ok()).toBeFalsy();
    expect(res.status()).toBe(401);
  });

  test('rejects login with non-existent phone', async ({ request }) => {
    const res = await request.post('auth/login', {
      data: { phone: '0900000001', password: 'Test@1234!' },
    });

    expect(res.ok()).toBeFalsy();
    expect(res.status()).toBe(401);
  });
});
