<?php


require_once '../../core/api_bootstrap.php';
require_once '../../core/provider_auth.php';

$provider_id = ProviderAuth::id();

$data = Request::get();

try {

    Validator::required($data, ['provider_id']);

    $db = new Database();
    $conn = $db->connect();

    /*
    Today's leads
    */

    $stmt = $conn->prepare("

SELECT COUNT(*)

FROM leads_distribution

WHERE provider_id=:provider
AND DATE(sent_at)=CURDATE()

");

    $stmt->execute([
        ':provider' => $data['provider_id']
    ]);

    $today = $stmt->fetchColumn();

    /*
    Total leads
    */

    $stmt = $conn->prepare("

SELECT COUNT(*)

FROM leads_distribution

WHERE provider_id=:provider

");

    $stmt->execute([
        ':provider' => $data['provider_id']
    ]);

    $total = $stmt->fetchColumn();

    /*
    Subscription status
    */

    $stmt = $conn->prepare("

SELECT plan_name,expiry_date,daily_lead_limit

FROM provider_subscriptions

WHERE provider_id=:provider
AND status='active'

LIMIT 1

");

    $stmt->execute([
        ':provider' => $data['provider_id']
    ]);

    $plan = $stmt->fetch();

    Response::success([

        "today_leads" => $today,
        "total_leads" => $total,
        "plan" => $plan

    ]);

} catch (Exception $e) {

    Logger::log("system.log", $e->getMessage());

    Response::error("Server error", 500);

}