import * as companyService from '../services/company.service.js';
import * as companyEmailService from '../services/company-email.service.js';

export const getAllCompanies = async (req, res, next) => {
  try {
    const { search } = req.query;
    const userId = req.user?.id;
    const userRole = req.user?.role;

    const companies = await companyService.getAllCompanies(search, userId, userRole);

    res.status(200).json({
      success: true,
      count: companies.length,
      data: companies,
    });
  } catch (error) {
    next(error);
  }
};

export const getCompanyById = async (req, res, next) => {
  try {
    const { companyId } = req.params;

    const company = await companyService.getCompanyById(companyId);

    res.status(200).json({
      success: true,
      data: company,
    });
  } catch (error) {
    next(error);
  }
};

export const getCompanyEmailThreads = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { companyId } = req.params;

    await companyService.getCompanyById(companyId);

    const result = await companyEmailService.getCompanyEmailThreads(companyId, userId);

    res.status(200).json({
      success: true,
      data: result,
    });
  } catch (error) {
    next(error);
  }
};

export const getCompanyUnreadCounts = async (req, res, next) => {
  try {
    const userId = req.userId;
    const counts = await companyEmailService.getCompanyUnreadCounts(userId);

    res.status(200).json({
      success: true,
      data: counts,
    });
  } catch (error) {
    next(error);
  }
};

export const getCompanyWithRelations = async (req, res, next) => {
  try {
    const { companyId } = req.params;

    const company = await companyService.getCompanyWithRelations(companyId);

    res.status(200).json({
      success: true,
      data: company,
    });
  } catch (error) {
    next(error);
  }
};

export const createCompany = async (req, res, next) => {
  try {
    const userId = req.userId;
    const data = req.body;

    const company = await companyService.createCompany(userId, data, { userId, ipAddress: req.ip, userAgent: req.headers['user-agent'] });

    res.status(201).json({
      success: true,
      data: company,
      message: 'Firma bola vytvorená',
    });
  } catch (error) {
    next(error);
  }
};

export const updateCompany = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { companyId } = req.params;
    const data = req.body;

    const company = await companyService.updateCompany(companyId, data, { userId, ipAddress: req.ip, userAgent: req.headers['user-agent'] });

    res.status(200).json({
      success: true,
      data: company,
      message: 'Firma bola aktualizovaná',
    });
  } catch (error) {
    next(error);
  }
};

export const deleteCompany = async (req, res, next) => {
  try {
    const { companyId } = req.params;
    const userId = req.userId;

    const result = await companyService.deleteCompany(companyId, { userId, ipAddress: req.ip, userAgent: req.headers['user-agent'] });

    res.status(200).json({
      success: true,
      message: result.message,
    });
  } catch (error) {
    next(error);
  }
};
