// src/controllers/investmentController.js

const investmentModel = require('../models/investmentModel');

/**
 * GET /investments
 */
const getAllInvestments = (req, res) => {
  investmentModel.getAllInvestments((err, rows) => {
    if (err) {
      return res.status(500).json({ error: 'Erro ao buscar investimentos.' });
    }
    return res.json(rows); // array de investments
  });
};

/**
 * GET /investments/:id
 */
const getInvestmentById = (req, res) => {
  const investment_id = parseInt(req.params.id, 10);

  if (!investment_id) {
    return res.status(400).json({ error: 'ID inválido.' });
  }

  investmentModel.getInvestmentById(investment_id, (err, row) => {
    if (err) {
      return res.status(500).json({ error: 'Erro ao buscar investimento.' });
    }
    if (!row) {
      return res.status(404).json({ error: 'Investimento não encontrado.' });
    }
    return res.json(row);
  });
};

function getRentabilityByInvestmentId(req, res) {
  const { id } = req.params;

  investmentModel.getRentabilityByInvestmentId(id, (err, rentability) => {
    if (err) return res.status(500).json({ error: 'Erro ao buscar rentabilidade.' });
    if (rentability === null) return res.status(404).json({ error: 'Investimento não encontrado.' });
    
    return res.json(rentability);
  });
}


/**
 * POST /investments
 * Body: { user_api_key, investment_type_name, investment_start, investment_end, investment_amt }
 */
const createInvestment = (req, res) => {
  const { user_api_key, investment_type_name, investment_start, investment_end, investment_amt } = req.body;

  // Validação simples
  if (!user_api_key || !investment_type_name || !investment_start || !investment_end) {
    return res.status(400).json({
      error: 'Campos obrigatórios: user_api_key, investment_type_name, investment_start, investment_end, investment_amt'
    });
  }

  investmentModel.createInvestment({ user_api_key, investment_type_name, investment_start, investment_end, investment_amt }, (err, newId) => {
    if (err) {
      return res.status(400).json({ error: err.message });
    }
    return res.status(201).json({
      message: 'Investimento criado com sucesso!',
      investment_id: newId
    });
  });
};

/**
 * PUT /investments/:id
 * Body: { user_api_key, investment_type_name, investment_start, investment_end, investment_amt }
 */
const updateInvestment = (req, res) => {
  const investment_id = parseInt(req.params.id, 10);
  const { user_api_key, investment_type_name, investment_start, investment_end, investment_amt } = req.body;

  if (!investment_id) {
    return res.status(400).json({ error: 'ID inválido na URL.' });
  }
  if (!user_api_key || !investment_type_name || !investment_start || !investment_end || !investment_amt) {
    return res.status(400).json({
      error: 'Campos obrigatórios: user_api_key, investment_type_name, investment_start, investment_end, investment_amt.'
    });
  }

  investmentModel.updateInvestment(investment_id, { user_api_key, investment_type_name, investment_start, investment_end, investment_amt }, (err, changes) => {
    if (err) {
      return res.status(400).json({ error: err.message });
    }
    if (changes === 0) {
      return res.status(404).json({ error: 'Investimento não encontrado ou sem alterações.' });
    }
    return res.json({ message: 'Investimento atualizado com sucesso!' });
  });
};

/**
 * DELETE /investments/:id
 */
const deleteInvestment = (req, res) => {
  const investment_id = parseInt(req.params.id, 10);

  if (!investment_id) {
    return res.status(400).json({ error: 'ID inválido na URL.' });
  }

  investmentModel.deleteInvestment(investment_id, (err, changes) => {
    if (err) {
      return res.status(500).json({ error: 'Erro ao remover investimento.' });
    }
    if (changes === 0) {
      return res.status(404).json({ error: 'Investimento não encontrado.' });
    }
    return res.json({ message: 'Investimento removido com sucesso!' });
  });
};

module.exports = {
  getAllInvestments,
  getInvestmentById,
  getRentabilityByInvestmentId,
  createInvestment,
  updateInvestment,
  deleteInvestment
};
