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 { formatErrorResponse } from '../utils/errors.js';

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

    const companies = await companyService.getAllCompanies(search);

    res.status(200).json({
      success: true,
      count: companies.length,
      data: companies,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

    const company = await companyService.getCompanyById(companyId);

    res.status(200).json({
      success: true,
      data: company,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

/**
 * Get company with relations (projects, todos, notes)
 * GET /api/companies/:companyId/details
 */
export const getCompanyWithRelations = async (req, res) => {
  try {
    const { companyId } = req.params;

    const company = await companyService.getCompanyWithRelations(companyId);

    res.status(200).json({
      success: true,
      data: company,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

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

    res.status(201).json({
      success: true,
      data: company,
      message: 'Firma bola vytvorená',
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

/**
 * Update company
 * PATCH /api/companies/:companyId
 * Body: { name, description, address, city, country, phone, email, website }
 */
export const updateCompany = async (req, res) => {
  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) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

    const result = await companyService.deleteCompany(companyId);

    res.status(200).json({
      success: true,
      message: result.message,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

    const notes = await noteService.getNotesByCompanyId(companyId);

    res.status(200).json({
      success: true,
      count: notes.length,
      data: notes,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

    const note = await noteService.createNote(userId, {
      content,
      companyId,
      reminderDate: reminderAt, // Map reminderAt to reminderDate
    });

    res.status(201).json({
      success: true,
      data: note,
      message: 'Poznámka bola pridaná',
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

    const note = await noteService.updateNote(noteId, {
      content,
      reminderDate: reminderAt, // Map reminderAt to reminderDate
    });

    res.status(200).json({
      success: true,
      data: note,
      message: 'Poznámka bola aktualizovaná',
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

    const result = await noteService.deleteNote(noteId);

    res.status(200).json({
      success: true,
      message: result.message,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

    const reminders = await companyReminderService.getRemindersByCompanyId(companyId);

    res.status(200).json({
      success: true,
      count: reminders.length,
      data: reminders,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

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

    res.status(201).json({
      success: true,
      data: reminder,
      message: 'Reminder bol pridaný',
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

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

    res.status(200).json({
      success: true,
      data: reminder,
      message: 'Reminder bol aktualizovaný',
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

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

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

    res.status(200).json({
      success: true,
      message: result.message,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};
