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

vi.mock('../../lib/prisma', () => ({ prisma: {} }))
vi.mock('../../lib/redis', () => ({
  redis: { on: vi.fn(), get: vi.fn(), set: vi.fn(), del: vi.fn(), quit: vi.fn() },
}))
vi.mock('../../middleware/requireCompanyAuth', () => ({
  requireCompanyAuth: (req: any, _res: any, next: any) => {
    req.employee = { id: 'employee_1', role: 'OWNER' }
    next()
  },
  requireCompanyDocumentAuth: (req: any, _res: any, next: any) => {
    req.employee = { id: 'employee_1', role: 'OWNER' }
    next()
  },
}))
vi.mock('../../middleware/requireTenant', () => ({
  requireTenant: (req: any, _res: any, next: any) => {
    req.companyId = 'company_1'
    next()
  },
}))
vi.mock('../../middleware/requireSubscription', () => ({ requireSubscription: (_req: any, _res: any, next: any) => next() }))
vi.mock('../../middleware/requireRole', () => ({ requireRole: () => (_req: any, _res: any, next: any) => next() }))
vi.mock('../../modules/team/team.service', () => ({ inviteEmployee: vi.fn(), updateEmployeeRole: vi.fn(), getMembers: vi.fn(), getMemberStats: vi.fn() }))
vi.mock('../../modules/notifications/notification.service', () => ({ setPreferences: vi.fn(), getPreferences: vi.fn(), listCompany: vi.fn(), countUnread: vi.fn() }))
vi.mock('../../modules/analytics/analytics.service', () => ({ getReport: vi.fn(), getSummary: vi.fn(), getDashboard: vi.fn(), getSources: vi.fn() }))

import request from 'supertest'
import { createApp } from '../../app'
import * as teamService from '../../modules/team/team.service'
import * as notificationService from '../../modules/notifications/notification.service'

const app = createApp()

describe('operations validation e2e smoke', () => {
  it('rejects malformed team and notification payloads before mocked services run', async () => {
    const badInvite = await request(app).post('/api/v1/team/invite').send({
      firstName: 'Aya',
      lastName: 'Benali',
      email: 'not-email',
      role: 'AGENT',
    })
    expect(badInvite.status).toBe(400)
    expect(teamService.inviteEmployee).not.toHaveBeenCalled()

    const badPrefs = await request(app).patch('/api/v1/notifications/company/preferences').send([
      { notificationType: 'BOOKING_CREATED', channel: 'EMAIL', enabled: 'yes-human-chaos' },
    ])
    expect(badPrefs.status).toBe(400)
    expect(notificationService.setPreferences).not.toHaveBeenCalled()
  })
})
