<?php

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

$data = Request::get();

try {

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

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

    $provider_id = (int)$data['provider_id'];



    /*
    --------------------------------
    PROVIDER + PROFILE
    --------------------------------
    */

    $query = "

        SELECT

            p.id,
            p.name,
            p.phone,
            p.whatsapp,
            p.email,
            p.account_status,
            p.kyc_status,
            p.created_at,

            pp.business_name,
            pp.description,
            pp.address,
            pp.city_id,
            pp.pincode,
            pp.profile_photo,
            pp.slug,
            pp.meta_title,
            pp.meta_description

        FROM providers p

        LEFT JOIN provider_profiles pp
        ON p.id = pp.provider_id

        WHERE p.id = :provider_id

        LIMIT 1

    ";

    $stmt = $conn->prepare($query);
    $stmt->execute([':provider_id'=>$provider_id]);

    $provider = $stmt->fetch();

    if(!$provider){
        Response::error("Provider not found",404);
    }



    /*
    --------------------------------
    PROVIDER SERVICES
    --------------------------------
    */

    $query = "

        SELECT

            psc.category_id,
            c.name AS category_name,

            psc.sub_category_id,
            sc.name AS subcategory_name

        FROM provider_service_categories psc

        JOIN categories c
        ON psc.category_id = c.id

        JOIN sub_categories sc
        ON psc.sub_category_id = sc.id

        WHERE psc.provider_id = :provider_id

    ";

    $stmt = $conn->prepare($query);
    $stmt->execute([':provider_id'=>$provider_id]);

    $services = $stmt->fetchAll();



    /*
    --------------------------------
    SERVICE AREAS
    --------------------------------
    */

    $query = "

        SELECT

            psa.id,
            psa.pincode_id,
            p.pincode,
            p.area_name,
            c.name AS city_name,
            s.name AS state_name

        FROM provider_service_areas psa

        JOIN pincodes p
        ON psa.pincode_id = p.id

        JOIN cities c
        ON p.city_id = c.id

        JOIN states s
        ON c.state_id = s.id

        WHERE psa.provider_id = :provider_id

    ";

    $stmt = $conn->prepare($query);
    $stmt->execute([':provider_id'=>$provider_id]);

    $areas = $stmt->fetchAll();



    /*
    --------------------------------
    ACTIVE SUBSCRIPTION
    --------------------------------
    */

    $query = "

        SELECT

            plan_id,
            plan_name,
            plan_price,
            categories_allowed,
            service_areas_allowed,
            daily_lead_limit,
            validity_days,
            start_date,
            expiry_date,
            status

        FROM provider_subscriptions

        WHERE provider_id = :provider_id
        AND status = 'active'
        AND expiry_date >= CURDATE()

        ORDER BY id DESC
        LIMIT 1

    ";

    $stmt = $conn->prepare($query);
    $stmt->execute([':provider_id'=>$provider_id]);

    $subscription = $stmt->fetch();



    /*
    --------------------------------
    TODAY'S LEAD USAGE
    --------------------------------
    */

    $query = "

        SELECT
            leads_received

        FROM provider_lead_usage

        WHERE provider_id = :provider_id
        AND usage_date = CURDATE()

        LIMIT 1

    ";

    $stmt = $conn->prepare($query);
    $stmt->execute([':provider_id'=>$provider_id]);

    $usage = $stmt->fetch();



    /*
    --------------------------------
    RESPONSE
    --------------------------------
    */

    Response::success([

        "provider" => $provider,
        "services" => $services,
        "service_areas" => $areas,
        "subscription" => $subscription,
        "today_leads" => $usage['leads_received'] ?? 0

    ]);

} catch (Exception $e) {

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

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

}