<?php
require_once '../includes/auth.php';

$auth = new Auth();
$auth->requireLogin();

header('Content-Type: application/json');

$method = $_SERVER['REQUEST_METHOD'];

try {
    require_once '../config/database.php';
    $database = new Database();
    $db = $database->getConnection();
    
    switch ($method) {
        case 'DELETE':
            $input = json_decode(file_get_contents('php://input'), true);
            $action = $input['action'] ?? '';
            $report_id = $input['report_id'] ?? '';
            
            if ($action === 'delete_report' && !empty($report_id)) {
                $query = "DELETE FROM seo_reports WHERE id = :report_id";
                $stmt = $db->prepare($query);
                $stmt->bindParam(':report_id', $report_id);
                $stmt->execute();
                
                if ($stmt->rowCount() > 0) {
                    echo json_encode([
                        'success' => true,
                        'message' => '報告刪除成功'
                    ]);
                } else {
                    echo json_encode([
                        'success' => false,
                        'message' => '找不到指定的報告'
                    ]);
                }
            } else {
                echo json_encode([
                    'success' => false,
                    'message' => '無效的操作'
                ]);
            }
            break;
            
        case 'GET':
            $action = $_GET['action'] ?? '';
            $report_id = $_GET['report_id'] ?? '';
            
            switch ($action) {
                case 'get_report':
                    if (!empty($report_id)) {
                        $query = "SELECT r.*, b.name as brand_name FROM seo_reports r 
                                  LEFT JOIN brands b ON r.brand_id = b.id 
                                  WHERE r.id = :report_id";
                        $stmt = $db->prepare($query);
                        $stmt->bindParam(':report_id', $report_id);
                        $stmt->execute();
                        $report = $stmt->fetch(PDO::FETCH_ASSOC);
                        
                        if ($report) {
                            echo json_encode([
                                'success' => true,
                                'data' => $report
                            ]);
                        } else {
                            echo json_encode([
                                'success' => false,
                                'message' => '找不到指定的報告'
                            ]);
                        }
                    } else {
                        echo json_encode([
                            'success' => false,
                            'message' => '報告ID不能為空'
                        ]);
                    }
                    break;
                    
                case 'list_reports':
                    $query = "SELECT r.*, b.name as brand_name FROM seo_reports r 
                              LEFT JOIN brands b ON r.brand_id = b.id 
                              ORDER BY r.created_at DESC";
                    $stmt = $db->prepare($query);
                    $stmt->execute();
                    $reports = $stmt->fetchAll(PDO::FETCH_ASSOC);
                    
                    echo json_encode([
                        'success' => true,
                        'data' => $reports
                    ]);
                    break;
                    
                default:
                    echo json_encode([
                        'success' => false,
                        'message' => '無效的操作'
                    ]);
                    break;
            }
            break;
            
        default:
            echo json_encode([
                'success' => false,
                'message' => '不支援的請求方法'
            ]);
            break;
    }
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => '操作失敗: ' . $e->getMessage()
    ]);
}
?>
