/**
 * @ai-summary Unit tests for VehiclesRepository
 * @ai-context Tests repository data access methods with mocked database
 */

import { Pool } from 'pg';
import { VehiclesRepository } from '../../data/vehicles.repository';

describe('VehiclesRepository', () => {
  let pool: Pool;
  let repository: VehiclesRepository;

  beforeEach(() => {
    pool = {
      query: jest.fn(),
    } as any;
    repository = new VehiclesRepository(pool);
  });

  describe('countByUserId', () => {
    it('returns accurate count of active vehicles', async () => {
      const mockResult = {
        rows: [{ count: '3' }],
      };
      (pool.query as jest.Mock).mockResolvedValue(mockResult);

      const count = await repository.countByUserId('user-123');

      expect(count).toBe(3);
      expect(pool.query).toHaveBeenCalledWith(
        expect.stringContaining('SELECT COUNT(*) as count FROM vehicles'),
        ['user-123']
      );
      expect(pool.query).toHaveBeenCalledWith(
        expect.stringContaining('is_active = true'),
        ['user-123']
      );
    });

    it('returns 0 for user with no vehicles', async () => {
      const mockResult = {
        rows: [{ count: '0' }],
      };
      (pool.query as jest.Mock).mockResolvedValue(mockResult);

      const count = await repository.countByUserId('user-empty');

      expect(count).toBe(0);
    });

    it('only counts active vehicles (excludes deleted)', async () => {
      const mockResult = {
        rows: [{ count: '2' }],
      };
      (pool.query as jest.Mock).mockResolvedValue(mockResult);

      const count = await repository.countByUserId('user-with-deleted');

      expect(count).toBe(2);
      expect(pool.query).toHaveBeenCalledWith(
        expect.stringContaining('is_active = true'),
        ['user-with-deleted']
      );
    });
  });
});
