/**
 * @ai-summary Integration tests for platform feature
 * @ai-context End-to-end API tests with authentication
 */
import { FastifyInstance } from 'fastify';
import { buildApp } from '../../../../app';

describe('Platform Integration Tests', () => {
  let app: FastifyInstance;
  let authToken: string;

  beforeAll(async () => {
    app = await buildApp();
    await app.ready();

    authToken = 'Bearer mock-jwt-token';
  });

  afterAll(async () => {
    await app.close();
  });

  describe('GET /api/platform/years', () => {
    it('should return 401 without authentication', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/years'
      });

      expect(response.statusCode).toBe(401);
    });

    it('should return list of years with authentication', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/years',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(200);
      const years = JSON.parse(response.payload);
      expect(Array.isArray(years)).toBe(true);
    });
  });

  describe('GET /api/platform/makes', () => {
    it('should return 401 without authentication', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/makes?year=2024'
      });

      expect(response.statusCode).toBe(401);
    });

    it('should return 400 for invalid year', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/makes?year=invalid',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(400);
    });

    it('should return makes for valid year with authentication', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/makes?year=2024',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(200);
      const data = JSON.parse(response.payload);
      expect(data).toHaveProperty('makes');
      expect(Array.isArray(data.makes)).toBe(true);
    });
  });

  describe('GET /api/platform/vehicle', () => {
    it('should return 401 without authentication', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/vehicle?vin=1HGCM82633A123456'
      });

      expect(response.statusCode).toBe(401);
    });

    it('should return 400 for invalid VIN format', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/vehicle?vin=INVALID',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(400);
      const data = JSON.parse(response.payload);
      expect(data.success).toBe(false);
      expect(data.error).toContain('17 characters');
    });

    it('should return 400 for VIN with invalid characters', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/vehicle?vin=1HGCM82633A12345I',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(400);
      const data = JSON.parse(response.payload);
      expect(data.success).toBe(false);
      expect(data.error).toContain('invalid characters');
    });

    it('should decode valid VIN with authentication', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/vehicle?vin=1HGCM82633A123456',
        headers: {
          authorization: authToken
        }
      });

      const data = JSON.parse(response.payload);
      expect(data).toHaveProperty('vin');
      expect(data).toHaveProperty('success');
      expect(data).toHaveProperty('result');
    });
  });

  describe('GET /api/platform/models', () => {
    it('should return 400 for missing make_id', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/models?year=2024',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(400);
    });

    it('should return models for valid year and make_id', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/models?year=2024&make_id=1',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(200);
      const data = JSON.parse(response.payload);
      expect(data).toHaveProperty('models');
      expect(Array.isArray(data.models)).toBe(true);
    });
  });

  describe('GET /api/platform/trims', () => {
    it('should return 400 for missing model_id', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/trims?year=2024',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(400);
    });

    it('should return trims for valid year and model_id', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/trims?year=2024&model_id=101',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(200);
      const data = JSON.parse(response.payload);
      expect(data).toHaveProperty('trims');
      expect(Array.isArray(data.trims)).toBe(true);
    });
  });

  describe('GET /api/platform/engines', () => {
    it('should return 400 for missing trim_id', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/engines?year=2024&model_id=101',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(400);
    });

    it('should return engines for valid year, model_id, and trim_id', async () => {
      const response = await app.inject({
        method: 'GET',
        url: '/api/platform/engines?year=2024&model_id=101&trim_id=1001',
        headers: {
          authorization: authToken
        }
      });

      expect(response.statusCode).toBe(200);
      const data = JSON.parse(response.payload);
      expect(data).toHaveProperty('engines');
      expect(Array.isArray(data.engines)).toBe(true);
    });
  });
});
