// src/controllers/investmentRentabilityController.js

const investmentRentModel = require('../models/investmentRentabilityModel');

/**
 * GET /investment-rentabilities
 */
const getAllRentabilities = (req, res) => {
  investmentRentModel.getAllRentabilities((err, rows) => {
    if (err) {
      return res.status(500).json({ error: 'Erro ao buscar rentabilities.' });
    }
    return res.json(rows); // retorna array de registros
  });
};

/**
 * GET /investment-rentabilities/:id
 */
const getRentabilityById = (req, res) => {
  const investment_rentability_id = parseInt(req.params.id, 10);

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

  investmentRentModel.getRentabilityById(investment_rentability_id, (err, row) => {
    if (err) {
      return res.status(500).json({ error: 'Erro ao buscar rentability by ID.' });
    }
    if (!row) {
      return res.status(404).json({ error: 'Rentability não encontrada.' });
    }
    return res.json(row);
  });
};

/**
 * POST /investment-rentabilities
 * Body: { investment_type_name, btcv_start_date, btcv_end_date, investment_rentability_api_rent }
 */
const createRentability = (req, res) => {
  const { investment_type_name, btcv_start_date, btcv_end_date, investment_rentability_api_rent } = req.body;

  // Validações simples
  if (!investment_type_name || !btcv_start_date || !btcv_end_date || !investment_rentability_api_rent) {
    return res.status(400).json({
      error: 'Campos obrigatórios: investment_type_name, btcv_start_date, btcv_end_date, investment_rentability_api_rent.'
    });
  }

  investmentRentModel.createRentability(
    { investment_type_name, btcv_start_date, btcv_end_date, investment_rentability_api_rent },
    (err, newId) => {
      if (err) {
        // Se vier Error(...) de "Tipo de investimento não encontrado", etc.
        return res.status(400).json({ error: err.message });
      }
      return res.status(201).json({
        message: 'investment_rentability criada com sucesso!',
        investment_rentability_id: newId
      });
    }
  );
};

/**
 * PUT /investment-rentabilities/:id
 * Body: { investment_type_name, btcv_start_date, btcv_end_date, investment_rentability_api_rent }
 */
const updateRentability = (req, res) => {
  const investment_rentability_id = parseInt(req.params.id, 10);
  const { investment_type_name, btcv_start_date, btcv_end_date, investment_rentability_api_rent } = req.body;

  if (!investment_rentability_id) {
    return res.status(400).json({ error: 'ID inválido na URL.' });
  }
  if (!investment_type_name || !btcv_start_date || !btcv_end_date || !investment_rentability_api_rent) {
    return res.status(400).json({
      error: 'Campos obrigatórios: investment_type_name, btcv_start_date, btcv_end_date, investment_rentability_api_rent.'
    });
  }

  investmentRentModel.updateRentability(
    investment_rentability_id,
    { investment_type_name, btcv_start_date, btcv_end_date, investment_rentability_api_rent },
    (err, changes) => {
      if (err) {
        return res.status(400).json({ error: err.message });
      }
      if (changes === 0) {
        return res.status(404).json({ error: 'Rentability não encontrada ou sem alterações.' });
      }
      return res.json({ message: 'investment_rentability atualizada com sucesso!' });
    }
  );
};

/**
 * DELETE /investment-rentabilities/:id
 */
const deleteRentability = (req, res) => {
  const investment_rentability_id = parseInt(req.params.id, 10);

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

  investmentRentModel.deleteRentability(investment_rentability_id, (err, changes) => {
    if (err) {
      return res.status(500).json({ error: 'Erro ao remover rentability.' });
    }
    if (changes === 0) {
      return res.status(404).json({ error: 'Rentability não encontrada.' });
    }
    return res.json({ message: 'investment_rentability removida com sucesso!' });
  });
};

module.exports = {
  getAllRentabilities,
  getRentabilityById,
  createRentability,
  updateRentability,
  deleteRentability
};
