<?php
/**
 * Tom's Java Jive - Loyalty Points API
 */

header('Content-Type: application/json');

require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/loyalty.php';

if (!CustomerAuth::isLoggedIn()) {
    jsonResponse(['error' => 'Authentication required'], 401);
}

$customer = CustomerAuth::getUser();
$method = $_SERVER['REQUEST_METHOD'];
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? $_GET['action'] ?? '';

switch ($action) {
    case 'status':
        // Get customer's loyalty status
        $status = loyalty()->getCustomerTier($customer['customer_id']);
        $conversion = loyalty()->getConversionInfo();
        
        jsonResponse([
            'tier' => $status['tier'],
            'tier_name' => $status['info']['name'],
            'tier_color' => $status['info']['color'],
            'tier_icon' => $status['info']['icon'],
            'benefits' => $status['info']['benefits'],
            'multiplier' => $status['info']['multiplier'],
            'points' => $status['points'],
            'lifetime_points' => $status['lifetime_points'],
            'points_value' => $status['points'] * $conversion['points_value'],
            'next_tier' => $status['next_tier'],
            'next_tier_name' => $status['next_tier_info']['name'] ?? null,
            'points_to_next' => $status['points_to_next'],
            'progress_percent' => $status['progress_percent'],
            'conversion' => $conversion
        ]);
        break;
        
    case 'history':
        // Get loyalty transaction history
        $limit = min(50, intval($_GET['limit'] ?? 20));
        $history = loyalty()->getHistory($customer['customer_id'], $limit);
        
        jsonResponse(['transactions' => $history]);
        break;
        
    case 'redeem':
        // Redeem points for credit
        if ($method !== 'POST') {
            jsonResponse(['error' => 'POST required'], 405);
        }
        
        $points = intval($input['points'] ?? 0);
        
        if ($points < 100) {
            jsonResponse(['error' => 'Minimum 100 points required for redemption'], 400);
        }
        
        $result = loyalty()->redeemPoints($customer['customer_id'], $points);
        
        if ($result['success']) {
            jsonResponse([
                'success' => true,
                'points_redeemed' => $result['points_redeemed'],
                'credit_value' => $result['credit_value'],
                'new_points_balance' => $result['new_points_balance'],
                'new_wallet_balance' => $result['new_wallet_balance'],
                'message' => 'Successfully redeemed ' . $points . ' points for ' . formatCurrency($result['credit_value'])
            ]);
        } else {
            jsonResponse(['error' => $result['error']], 400);
        }
        break;
        
    case 'tiers':
        // Get all tier information
        $tiers = loyalty()->getTiers();
        $conversion = loyalty()->getConversionInfo();
        
        jsonResponse([
            'tiers' => $tiers,
            'conversion' => $conversion
        ]);
        break;
        
    default:
        jsonResponse(['error' => 'Invalid action'], 400);
}
