import { Router } from 'express'
import { requireCompanyAuth, requireCompanyDocumentAuth } from '../../middleware/requireCompanyAuth'
import { requireTenant } from '../../middleware/requireTenant'
import { requireSubscription } from '../../middleware/requireSubscription'
import { requireRole } from '../../middleware/requireRole'
import { parseBody, parseQuery, parseParams } from '../../http/validate'
import { ok, created } from '../../http/respond'
import { imageUpload, assertImageFile } from '../../http/upload'
import * as service from './customer.service'
import { customerSchema, listQuerySchema, approveLicenseSchema, flagSchema, idParamSchema } from './customer.schemas'

const router = Router()

router.get('/:id/license-image', requireCompanyDocumentAuth, requireTenant, requireSubscription, async (req, res, next) => {
  try {
    const { id } = parseParams(idParamSchema, req)
    const filePath = await service.getLicenseImageFile(id, req.companyId)
    res.sendFile(filePath)
  } catch (err) { next(err) }
})

router.use(requireCompanyAuth, requireTenant, requireSubscription)
router.get('/', async (req, res, next) => {
  try {
    const query = parseQuery(listQuerySchema, req)
    const result = await service.listCustomers(req.companyId, query)
    res.json(result)
  } catch (err) { next(err) }
})

router.post('/', async (req, res, next) => {
  try {
    const body = parseBody(customerSchema, req)
    const customer = await service.createCustomer(body, req.companyId)
    created(res, customer)
  } catch (err) { next(err) }
})

router.get('/:id', async (req, res, next) => {
  try {
    const { id } = parseParams(idParamSchema, req)
    const customer = await service.getCustomer(id, req.companyId)
    ok(res, customer)
  } catch (err) { next(err) }
})

router.patch('/:id', requireRole('MANAGER'), async (req, res, next) => {
  try {
    const { id } = parseParams(idParamSchema, req)
    const body = parseBody(customerSchema.partial(), req)
    const customer = await service.updateCustomer(id, req.companyId, body)
    ok(res, customer)
  } catch (err) { next(err) }
})

router.post('/:id/flag', requireRole('MANAGER'), async (req, res, next) => {
  try {
    const { id } = parseParams(idParamSchema, req)
    const { reason } = parseBody(flagSchema, req)
    await service.flagCustomer(id, req.companyId, reason)
    ok(res, { success: true })
  } catch (err) { next(err) }
})

router.delete('/:id/flag', requireRole('MANAGER'), async (req, res, next) => {
  try {
    const { id } = parseParams(idParamSchema, req)
    await service.unflagCustomer(id, req.companyId)
    ok(res, { success: true })
  } catch (err) { next(err) }
})

router.post('/:id/validate-license', async (req, res, next) => {
  try {
    const { id } = parseParams(idParamSchema, req)
    const result = await service.validateCustomerLicense(id, req.companyId)
    ok(res, result)
  } catch (err) { next(err) }
})

router.post('/:id/license-image', imageUpload.single('file'), async (req, res, next) => {
  try {
    assertImageFile(req.file, 'license image')
    const { id } = parseParams(idParamSchema, req)
    const updated = await service.uploadLicenseImage(id, req.companyId, req.file)
    ok(res, updated)
  } catch (err) { next(err) }
})

router.post('/:id/approve-license', requireRole('MANAGER'), async (req, res, next) => {
  try {
    const { id } = parseParams(idParamSchema, req)
    const { decision, note } = parseBody(approveLicenseSchema, req)
    const approverName = `${req.employee.firstName} ${req.employee.lastName}`
    const updated = await service.approveLicense(id, req.companyId, decision, note, approverName)
    ok(res, updated)
  } catch (err) { next(err) }
})

export default router
