import { Router, Response } from 'express';
import { requireAuth, AuthRequest } from '../../middleware/auth.middleware';
import { BankLoanService } from '../../services/bank-loan.service';
import { GangServiceUsageService } from '../../services/gang-service-usage.service';
import { BankLaunderService } from '../../services/bank-launder.service';

const router = Router();
router.use(requireAuth);

/** GET /bank/my-ceiling — Plafond de crédit banque calculé (fiches de paye + biens) */
router.get('/my-ceiling', async (req: AuthRequest, res: Response) => {
  try {
    const ceiling = await BankLoanService.computePlayerBankCeiling(req.playerId!);
    res.json({ ceiling });
  } catch (err: any) { res.status(400).json({ error: err.message }); }
});

/** POST /bank/poi/:poiId/loan — Demande de crédit banque */
router.post('/poi/:poiId/loan', async (req: AuthRequest, res: Response) => {
  try {
    const { amount, duration_days } = req.body || {};
    if (!amount || !duration_days) return res.status(400).json({ error: 'amount + duration_days requis' });
    const loan = await BankLoanService.requestBankLoan(req.playerId!, req.params.poiId, amount, duration_days);
    res.json(loan);
  } catch (err: any) { res.status(400).json({ error: err.message }); }
});

/** POST /bank/gang/:gangId/loan — Crédit gang (1000 ∞/mission) */
router.post('/gang/:gangId/loan', async (req: AuthRequest, res: Response) => {
  try {
    const { amount } = req.body || {};
    if (!amount) return res.status(400).json({ error: 'amount requis' });
    const loan = await GangServiceUsageService.requestCredit(req.playerId!, req.params.gangId, amount);
    res.json(loan);
  } catch (err: any) { res.status(400).json({ error: err.message }); }
});

/** POST /bank/cartel-peer/loan — Prêt inter-joueurs cartel */
router.post('/cartel-peer/loan', async (req: AuthRequest, res: Response) => {
  try {
    const { to_player_id, cartel_id, amount, rate_daily, duration_days } = req.body || {};
    if (!to_player_id || !cartel_id || !amount || rate_daily == null || !duration_days) {
      return res.status(400).json({ error: 'Paramètres manquants' });
    }
    const loan = await BankLoanService.requestCartelPeerLoan(
      req.playerId!, to_player_id, cartel_id, amount, rate_daily, duration_days,
    );
    res.json(loan);
  } catch (err: any) { res.status(400).json({ error: err.message }); }
});

/** POST /bank/loan/:loanId/repay — Rembourser (partiel ou total -20% intérêts restants) */
router.post('/loan/:loanId/repay', async (req: AuthRequest, res: Response) => {
  try {
    const { amount } = req.body || {};
    if (!amount) return res.status(400).json({ error: 'amount requis' });
    const result = await BankLoanService.repay(req.playerId!, req.params.loanId, amount);
    res.json(result);
  } catch (err: any) { res.status(400).json({ error: err.message }); }
});

/** GET /bank/loans/mine — Mes prêts (emprunteur) */
router.get('/loans/mine', async (req: AuthRequest, res: Response) => {
  try { res.json(await BankLoanService.listByBorrower(req.playerId!)); }
  catch (err: any) { res.status(400).json({ error: err.message }); }
});

/** GET /bank/loans/cartel/:cartelId — Prêts inter-membres du cartel */
router.get('/loans/cartel/:cartelId', async (req: AuthRequest, res: Response) => {
  try { res.json(await BankLoanService.listByCartel(req.params.cartelId)); }
  catch (err: any) { res.status(400).json({ error: err.message }); }
});

/** GET /bank/attackable-status — Suis-je attaquable par mes membres cartel ? */
router.get('/attackable-status', async (req: AuthRequest, res: Response) => {
  try { res.json(await BankLoanService.isAttackableByCartelPeers(req.playerId!)); }
  catch (err: any) { res.status(400).json({ error: err.message }); }
});

// Phase 4 (2026-04-20) — Blanchiment banque NPC (8%) / owned (0%)
/** POST /bank/poi/:poiId/launder body { amount_shadow } */
router.post('/poi/:poiId/launder', async (req: AuthRequest, res: Response) => {
  try {
    const { amount_shadow } = req.body || {};
    if (!amount_shadow) return res.status(400).json({ error: 'amount_shadow requis' });
    const r = await BankLaunderService.launder(req.playerId!, req.params.poiId, Number(amount_shadow));
    res.json(r);
  } catch (err: any) { res.status(400).json({ error: err.message }); }
});

/** GET /bank/poi/:poiId/launder-status */
router.get('/poi/:poiId/launder-status', async (req: AuthRequest, res: Response) => {
  try {
    const s = await BankLaunderService.getWeeklyStatus(req.playerId!, req.params.poiId);
    res.json(s);
  } catch (err: any) { res.status(400).json({ error: err.message }); }
});

export default router;
