<?php

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

$provider_id = ProviderAuth::id();

try{

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


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

$query="

SELECT

psc.category_id,
c.name AS category,

psc.sub_category_id,
sc.name AS subcategory

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

ORDER BY c.name, sc.name

";

$stmt=$conn->prepare($query);

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

$services=$stmt->fetchAll();



/*
-----------------------------------
SERVICE AREA MAPPING
-----------------------------------
*/

$query="

SELECT

psam.id,

psam.category_id,
c.name AS category,

psam.sub_category_id,
sc.name AS subcategory,

psam.pincode_id,
p.pincode,
p.area_name,

city.name AS city,
state.name AS state

FROM provider_service_area_map psam

JOIN categories c
ON psam.category_id = c.id

JOIN sub_categories sc
ON psam.sub_category_id = sc.id

JOIN pincodes p
ON psam.pincode_id = p.id

JOIN cities city
ON p.city_id = city.id

JOIN states state
ON city.state_id = state.id

WHERE psam.provider_id = :provider

ORDER BY sc.name, city.name

";

$stmt=$conn->prepare($query);

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

$areas=$stmt->fetchAll();



Response::success([

"services"=>$services,
"service_areas"=>$areas

]);

}
catch(Exception $e){

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

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

}