<?php
require_once '../config.php';
require_once '../../includes/Auth.php';
require_once '../../includes/Shop.php';

$auth = new Auth();
$user = $auth->requireAuth(); // Ensure user is logged in
$shop = new Shop();

// Ensure only shop owners or admins can access this endpoint
if ($user['role'] !== 'shop_owner' && $user['role'] !== 'admin') {
    jsonResponse(['success' => false, 'message' => 'Unauthorized: Only shop owners can view their shops.'], 403);
}

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    try {
        $shops = $shop->getUserShops($user['id']);
        
        jsonResponse([
            'success' => true,
            'shops' => $shops
        ]);
    } catch (Exception $e) {
        jsonResponse([
            'success' => false,
            'message' => $e->getMessage()
        ], 500);
    }
} else {
    jsonResponse(['success' => false, 'message' => 'Method not allowed'], 405);
}
?>