<?php

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

$provider_id = ProviderAuth::id();

$data = Request::post();

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

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


/* GET CATEGORY FROM SUBCATEGORY */

$stmt=$conn->prepare("
SELECT category_id
FROM sub_categories
WHERE id=:id
LIMIT 1
");

$stmt->execute([
':id'=>$data['sub_category_id']
]);

$sub=$stmt->fetch();

if(!$sub){
Response::error("Invalid service");
}


/* CHECK DUPLICATE */

$stmt=$conn->prepare("
SELECT id
FROM provider_service_categories
WHERE provider_id=:provider
AND sub_category_id=:sub
LIMIT 1
");

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

if($stmt->fetch()){
Response::error("Service already added");
}


/* INSERT */

$stmt=$conn->prepare("
INSERT INTO provider_service_categories
(provider_id,category_id,sub_category_id)

VALUES
(:provider,:category,:sub)
");

$stmt->execute([
':provider'=>$provider_id,
':category'=>$sub['category_id'],
':sub'=>$data['sub_category_id']
]);


Response::success([],"Service added");
