<?php
/**
 * GET /api/business/subscription.php
 * Business-only. Returns the caller's current subscription status - no
 * billing is live yet (per RFQ scope, no payment gateway), this is purely
 * informational: what free-trial window they're in, and whatever plan
 * interest they've expressed so far.
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/response.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/settings.php';

$userId = require_login();
require_role(['business']);

$pdo = Database::connect();
$stmt = $pdo->prepare(
    'SELECT subscription_status, subscription_plan, trial_ends_at, subscription_notes FROM business_profiles WHERE user_id = ?'
);
$stmt->execute([$userId]);
$sub = $stmt->fetch();

if (!$sub) {
    send_error('Business profile not found.', 404);
}

$daysLeft = null;
if ($sub['subscription_status'] === 'trial' && $sub['trial_ends_at']) {
    $diff = (strtotime($sub['trial_ends_at']) - strtotime(date('Y-m-d'))) / 86400;
    $daysLeft = (int) ceil($diff);
}

send_json(true, 'OK', [
    'subscription' => $sub,
    'trial_days_left' => $daysLeft,
    'subscriptions_enabled' => get_setting($pdo, 'subscriptions_enabled', '0') === '1',
]);
