/**
 * Brand Routes
 */

import express, { Request, Response } from 'express';
import { body, validationResult } from 'express-validator';
import { getModels } from '../models';
import { authenticateJwtOrApiToken } from '../middleware/dualAuth';
import { requirePermission } from '../middleware/permissions';

const router = express.Router();

router.use(authenticateJwtOrApiToken);

// Get brand for a company
router.get('/:companyId', async (req: Request, res: Response) => {
  try {
    const { companyId } = req.params;
    const { Brand } = getModels();

    if (!req.user!.companyIds.includes(companyId) && req.user!.role !== 'admin') {
      res.status(403).json({ error: 'Access denied' });
      return;
    }

    const brand = await Brand.findOne({ companyId });

    // Return null instead of 404 - no brand is a valid state for a new company
    res.json(brand);
  } catch (error) {
    res.status(500).json({ error: 'Failed to get brand' });
  }
});

// Create brand
router.post(
  '/',
  requirePermission('brand', 'edit'),
  [
    body('companyId').notEmpty().withMessage('Company ID is required'),
  ],
  async (req: Request, res: Response) => {
    try {
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        res.status(400).json({ errors: errors.array() });
        return;
      }

      if (!req.user!.companyIds.includes(req.body.companyId) && req.user!.role !== 'admin') {
        res.status(403).json({ error: 'Access denied for this company' });
        return;
      }

      const { Brand } = getModels();

      // Check if brand already exists
      const existing = await Brand.findOne({ companyId: req.body.companyId });
      if (existing) {
        res.status(400).json({ error: 'Brand already exists for this company' });
        return;
      }

      const brand = new Brand(req.body);
      await brand.save();

      res.status(201).json(brand);
    } catch (error) {
      res.status(500).json({ error: 'Failed to create brand' });
    }
  }
);

// Update brand
router.put('/:id', requirePermission('brand', 'edit'), async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const { Brand } = getModels();

    const brand = await Brand.findById(id);
    if (!brand) {
      res.status(404).json({ error: 'Brand not found' });
      return;
    }

    if (!req.user!.companyIds.includes(brand.companyId) && req.user!.role !== 'admin') {
      res.status(403).json({ error: 'Access denied' });
      return;
    }

    Object.assign(brand, req.body, { updatedAt: new Date().toISOString() });
    await brand.save();

    res.json(brand);
  } catch (error) {
    res.status(500).json({ error: 'Failed to update brand' });
  }
});

// Delete brand
router.delete('/:id', requirePermission('brand', 'delete'), async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const { Brand } = getModels();

    const brand = await Brand.findById(id);
    if (!brand) {
      res.status(404).json({ error: 'Brand not found' });
      return;
    }

    if (!req.user!.companyIds.includes(brand.companyId) && req.user!.role !== 'admin') {
      res.status(403).json({ error: 'Access denied' });
      return;
    }

    await Brand.findByIdAndDelete(id);

    res.json({ message: 'Brand deleted successfully' });
  } catch (error) {
    res.status(500).json({ error: 'Failed to delete brand' });
  }
});

export default router;
