import clubAccountService from '../services/clubAccountService.js';

class ClubAccountController {
  async listClubAccounts(req, res) {
    try {
      const { clubId } = req.params;
      const payload = await clubAccountService.listClubAccounts(Number(clubId));
      res.json(payload);
    } catch (error) {
      console.error('[listClubAccounts] - Error:', error);
      res.status(error?.status || 500).json({ error: error?.message || 'Konten konnten nicht geladen werden.' });
    }
  }

  async createClubAccount(req, res) {
    try {
      const { clubId } = req.params;
      const account = await clubAccountService.createClubAccount(Number(clubId), req.body || {});
      res.status(201).json({ account });
    } catch (error) {
      console.error('[createClubAccount] - Error:', error);
      res.status(error?.status || 500).json({ error: error?.message || 'Konto konnte nicht gespeichert werden.' });
    }
  }

  async updateClubAccount(req, res) {
    try {
      const { clubId, accountId } = req.params;
      const account = await clubAccountService.updateClubAccount(Number(clubId), Number(accountId), req.body || {});
      res.json({ account });
    } catch (error) {
      console.error('[updateClubAccount] - Error:', error);
      res.status(error?.status || 500).json({ error: error?.message || 'Konto konnte nicht gespeichert werden.' });
    }
  }

  async updateClubAccountStatus(req, res) {
    try {
      const { clubId, accountId } = req.params;
      const account = await clubAccountService.updateClubAccountStatus(Number(clubId), Number(accountId), String(req.body?.status || ''));
      res.json({ account });
    } catch (error) {
      console.error('[updateClubAccountStatus] - Error:', error);
      res.status(error?.status || 500).json({ error: error?.message || 'Kontostatus konnte nicht gespeichert werden.' });
    }
  }

  async deleteClubAccount(req, res) {
    try {
      const { clubId, accountId } = req.params;
      await clubAccountService.deleteClubAccount(Number(clubId), Number(accountId));
      res.json({ success: true });
    } catch (error) {
      console.error('[deleteClubAccount] - Error:', error);
      res.status(error?.status || 500).json({ error: error?.message || 'Konto konnte nicht gelöscht werden.' });
    }
  }

  async createTransaction(req, res) {
    try {
      const { clubId } = req.params;
      const transaction = await clubAccountService.createTransaction(Number(clubId), req.user?.id || null, req.body || {});
      res.status(201).json({ transaction });
    } catch (error) {
      console.error('[createClubAccountTransaction] - Error:', error);
      res.status(error?.status || 500).json({ error: error?.message || 'Kontenbewegung konnte nicht gespeichert werden.' });
    }
  }

  async updateTransaction(req, res) {
    try {
      const { clubId, transactionId } = req.params;
      const transaction = await clubAccountService.updateTransaction(Number(clubId), Number(transactionId), req.body || {});
      res.json({ transaction });
    } catch (error) {
      console.error('[updateClubAccountTransaction] - Error:', error);
      res.status(error?.status || 500).json({ error: error?.message || 'Kontenbewegung konnte nicht gespeichert werden.' });
    }
  }

  async deleteTransaction(req, res) {
    try {
      const { clubId, transactionId } = req.params;
      await clubAccountService.deleteTransaction(Number(clubId), Number(transactionId));
      res.json({ success: true });
    } catch (error) {
      console.error('[deleteClubAccountTransaction] - Error:', error);
      res.status(error?.status || 500).json({ error: error?.message || 'Kontenbewegung konnte nicht gelöscht werden.' });
    }
  }
}

export default new ClubAccountController();
