import express from 'express';
import {
  registerMember,
  getMemberProfile,
  getMemberDownline,
  updateMemberProfile,
  verifySponsor,
  getMemberDashboard,
  getMemberSalaryHistory,
  getMemberTree,          // ← NEW
} from '../controllers/memberController.js';
import { authenticateMember } from '../middleware/auth.js';
import { upload } from '../config/multer.js';
import { getIncomeHistory, getIncomeSummary } from '../controllers/member/income.js';

const router = express.Router();

// ── Auth / Registration ──────────────────────────────────────
router.post('/register', upload.fields([
  { name: 'profile_photo',  maxCount: 1 },
  { name: 'aadhaar_front',  maxCount: 1 },
  { name: 'aadhaar_back',   maxCount: 1 },
  { name: 'pan_image',      maxCount: 1 },
  { name: 'passbook_image', maxCount: 1 },
]), registerMember);

// ── Profile ──────────────────────────────────────────────────
router.get('/profile',                   authenticateMember, getMemberProfile);

router.put('/profile', authenticateMember, upload.fields([
  { name: 'profile_photo',  maxCount: 1 },
  { name: 'aadhaar_front',  maxCount: 1 },
  { name: 'aadhaar_back',   maxCount: 1 },
  { name: 'pan_image',      maxCount: 1 },
  { name: 'passbook_image', maxCount: 1 },
]), updateMemberProfile);


router.get('/downline',                  authenticateMember, getMemberDownline);
router.get('/verify-sponsor/:sponsorId',                     verifySponsor);

// ── Dashboard ────────────────────────────────────────────────
router.get('/dashboard',                 authenticateMember, getMemberDashboard);

// ── Income ───────────────────────────────────────────────────
router.get('/income/history/:memberId',  authenticateMember, getIncomeHistory);
router.get('/income/summary/:memberId',  authenticateMember, getIncomeSummary);

router.get('/:memberId/tree', getMemberTree);


// ── Salary ───────────────────────────────────────────────────
router.get('/salary/history',            authenticateMember, getMemberSalaryHistory);  // ← NEW

export default router;