import * as companyService from '../services/company.service.js';
import * as noteService from '../services/note.service.js';
import * as companyReminderService from '../services/company-reminder.service.js';
import * as companyEmailService from '../services/company-email.service.js';
import { logCompanyCreated, logCompanyDeleted } from '../services/audit.service.js';

/**
 * Get all companies
 * GET /api/companies?search=query
 */
export const getAllCompanies = async (req, res, next) => {
  try {
    const { search } = req.query;

    const companies = await companyService.getAllCompanies(search);

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

/**
 * Get company by ID
 * GET /api/companies/:companyId
 */
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) {
    return next(error);
  }
};

/**
 * Get company email threads aggregated across user's email accounts
 * GET /api/companies/:companyId/email-threads
 */
export const getCompanyEmailThreads = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { companyId } = req.params;

    // Ensure company exists
    await companyService.getCompanyById(companyId);

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

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

/**
 * Get unread email counts grouped by company for current user
 * GET /api/companies/email-unread
 */
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) {
    return next(error);
  }
};

/**
 * Get company with relations (projects, todos, notes)
 * GET /api/companies/:companyId/details
 */
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) {
    return next(error);
  }
};

/**
 * Create new company
 * POST /api/companies
 * Body: { name, description, address, city, country, phone, email, website }
 */
export const createCompany = async (req, res, next) => {
  try {
    const userId = req.userId;
    const data = req.body;

    const company = await companyService.createCompany(userId, data);

    // Log audit event
    await logCompanyCreated(userId, company.id, company.name, req.ip, req.headers['user-agent']);

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

/**
 * Update company
 * PATCH /api/companies/:companyId
 * Body: { name, description, address, city, country, phone, email, website }
 */
export const updateCompany = async (req, res, next) => {
  try {
    const { companyId } = req.params;
    const data = req.body;

    const company = await companyService.updateCompany(companyId, data);

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

/**
 * Delete company
 * DELETE /api/companies/:companyId
 */
export const deleteCompany = async (req, res, next) => {
  try {
    const { companyId } = req.params;
    const userId = req.userId;

    // Get company info before deleting
    const company = await companyService.getCompanyById(companyId);
    const companyName = company?.name;

    const result = await companyService.deleteCompany(companyId);

    // Log audit event
    await logCompanyDeleted(userId, companyId, companyName, req.ip, req.headers['user-agent']);

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

/**
 * Get company notes
 * GET /api/companies/:companyId/notes
 */
export const getCompanyNotes = async (req, res, next) => {
  try {
    const { companyId } = req.params;

    const notes = await noteService.getNotesByCompanyId(companyId);

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

/**
 * Add company note
 * POST /api/companies/:companyId/notes
 */
export const addCompanyNote = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { companyId } = req.params;
    const { content } = req.body;

    const note = await noteService.createNote(userId, {
      content,
      companyId,
    });

    res.status(201).json({
      success: true,
      data: note,
      message: 'Poznámka bola pridaná',
    });
  } catch (error) {
    return next(error);
  }
};

/**
 * Update company note
 * PATCH /api/companies/:companyId/notes/:noteId
 */
export const updateCompanyNote = async (req, res, next) => {
  try {
    const { noteId } = req.params;
    const { content } = req.body;

    const note = await noteService.updateNote(noteId, {
      content,
    });

    res.status(200).json({
      success: true,
      data: note,
      message: 'Poznámka bola aktualizovaná',
    });
  } catch (error) {
    return next(error);
  }
};

/**
 * Delete company note
 * DELETE /api/companies/:companyId/notes/:noteId
 */
export const deleteCompanyNote = async (req, res, next) => {
  try {
    const { noteId } = req.params;

    const result = await noteService.deleteNote(noteId);

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

/**
 * Company reminders
 * CRUD for /api/companies/:companyId/reminders
 */
export const getCompanyReminders = async (req, res, next) => {
  try {
    const { companyId } = req.params;

    const reminders = await companyReminderService.getRemindersByCompanyId(companyId);

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

export const createCompanyReminder = async (req, res, next) => {
  try {
    const { companyId } = req.params;
    const { description, dueDate, isChecked } = req.body;

    const reminder = await companyReminderService.createReminder(companyId, { description, dueDate, isChecked });

    res.status(201).json({
      success: true,
      data: reminder,
      message: 'Reminder bol pridaný',
    });
  } catch (error) {
    return next(error);
  }
};

export const updateCompanyReminder = async (req, res, next) => {
  try {
    const { companyId, reminderId } = req.params;
    const { description, dueDate, isChecked } = req.body;

    const reminder = await companyReminderService.updateReminder(companyId, reminderId, { description, dueDate, isChecked });

    res.status(200).json({
      success: true,
      data: reminder,
      message: 'Reminder bol aktualizovaný',
    });
  } catch (error) {
    return next(error);
  }
};

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

    const result = await companyReminderService.deleteReminder(companyId, reminderId);

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

export const getReminderSummary = async (_req, res, next) => {
  try {
    const summary = await companyReminderService.getReminderSummary();
    res.status(200).json({
      success: true,
      data: summary,
    });
  } catch (error) {
    return next(error);
  }
};

export const getReminderCountsByCompany = async (_req, res, next) => {
  try {
    const counts = await companyReminderService.getReminderCountsByCompany();
    res.status(200).json({
      success: true,
      data: counts,
    });
  } catch (error) {
    return next(error);
  }
};

export const getUpcomingReminders = async (_req, res, next) => {
  try {
    const reminders = await companyReminderService.getUpcomingReminders();
    res.status(200).json({
      success: true,
      count: reminders.length,
      data: reminders,
    });
  } catch (error) {
    return next(error);
  }
};
