import { describe, expect, it, vi } from 'vitest'

vi.mock('jsonwebtoken', () => ({ default: { verify: vi.fn(() => ({ sub: 'employee_1', type: 'employee' })) } }))
vi.mock('../../lib/redis', () => ({
  redis: { on: vi.fn(), get: vi.fn(), set: vi.fn(), del: vi.fn(), quit: vi.fn() },
}))
vi.mock('../../lib/prisma', () => ({
  prisma: {
    employee: { findUnique: vi.fn().mockResolvedValue({ id: 'employee_1', role: 'MANAGER', isActive: true, companyId: 'company_1', company: { id: 'company_1', status: 'ACTIVE' } }) },
    company: { findUnique: vi.fn().mockResolvedValue({ id: 'company_1', status: 'ACTIVE' }) },
    subscription: { findUnique: vi.fn().mockResolvedValue({ status: 'ACTIVE' }) },
  },
}))
vi.mock('../../modules/vehicles/vehicle.service', () => ({
  listVehicles: vi.fn(),
  getVehicle: vi.fn(),
  createVehicle: vi.fn(),
  updateVehicle: vi.fn(),
  deleteVehicle: vi.fn(),
  uploadPhotos: vi.fn(),
  deletePhoto: vi.fn(),
  setStatus: vi.fn(),
  setPublished: vi.fn(),
  checkAvailability: vi.fn(),
  getCalendar: vi.fn(),
  createCalendarBlock: vi.fn(),
  deleteCalendarBlock: vi.fn(),
  getMaintenanceLogs: vi.fn(),
  createMaintenanceLog: vi.fn(),
  getVehiclePricing: vi.fn(),
  updateVehiclePricing: vi.fn(),
  createVehiclePricingRule: vi.fn(),
  updateVehiclePricingRule: vi.fn(),
  deleteVehiclePricingRule: vi.fn(),
}))

import request from 'supertest'
import { createApp } from '../../app'
import * as vehicleService from '../../modules/vehicles/vehicle.service'

process.env.JWT_SECRET = 'test-secret'
const app = createApp()

describe('vehicle pricing e2e boundaries', () => {
  it('rejects anonymous and malformed pricing requests without invoking pricing services', async () => {
    const anonymous = await request(app).get('/api/v1/vehicles/vehicle_1/pricing')
    expect(anonymous.status).toBe(401)
    expect(vehicleService.getVehiclePricing).not.toHaveBeenCalled()

    const malformedConfig = await request(app)
      .put('/api/v1/vehicles/vehicle_1/pricing')
      .set('Authorization', 'Bearer employee-token')
      .send({ pricingMode: 'AUTOMATIC', baseDailyRate: -50 })

    expect(malformedConfig.status).toBe(400)
    expect(vehicleService.updateVehiclePricing).not.toHaveBeenCalled()

    const malformedRule = await request(app)
      .post('/api/v1/vehicles/vehicle_1/pricing/rules')
      .set('Authorization', 'Bearer employee-token')
      .send({ name: '', startDate: 'not-a-date', endDate: 'also-bad' })

    expect(malformedRule.status).toBe(400)
    expect(vehicleService.createVehiclePricingRule).not.toHaveBeenCalled()
  })
})
