<?php
// api/presenters/assign.php

require_once '../../config/cors.php';
require_once '../../config/db.php';
require_once '../../helpers/response.php';
require_once '../../helpers/request.php';
require_once '../../helpers/auth.php';

$method = getRequestMethod();

if ($method === 'OPTIONS') {
    http_response_code(200);
    exit;
}

$user = requireAdmin(); 

// POST - Assign presenter to podcast or media
if ($method === 'POST') {
    $data = getRequestData();
    
    validateRequiredFields($data, ['presenter_id', 'type', 'target_id']);
    
    $presenter_id = $data['presenter_id'];
    $type = $data['type']; // 'podcast' or 'media'
    $target_id = $data['target_id'];
    $role = isset($data['role']) ? $data['role'] : 'guest';
    
    try {
        // Check if presenter exists
        $stmt = $pdo->prepare("SELECT id FROM presenters WHERE id = :id");
        $stmt->execute([':id' => $presenter_id]);
        if (!$stmt->fetch()) {
            sendError('Presenter not found', 404);
        }
        
        if ($type === 'podcast') {
            // Check if podcast exists
            $stmt = $pdo->prepare("SELECT id FROM podcasts WHERE id = :id");
            $stmt->execute([':id' => $target_id]);
            if (!$stmt->fetch()) {
                sendError('Podcast not found', 404);
            }
            
            // Validate role
            $allowed_roles = ['host', 'co-host', 'guest'];
            if (!in_array($role, $allowed_roles)) {
                $role = 'guest';
            }
            
            // Check if already assigned
            $stmt = $pdo->prepare("
                SELECT id FROM podcast_presenters 
                WHERE podcast_id = :target_id AND presenter_id = :presenter_id
            ");
            $stmt->execute([
                ':target_id' => $target_id,
                ':presenter_id' => $presenter_id
            ]);
            
            if ($stmt->fetch()) {
                sendError('Presenter already assigned to this podcast', 400);
            }
            
            // Assign to podcast
            $stmt = $pdo->prepare("
                INSERT INTO podcast_presenters (podcast_id, presenter_id, role) 
                VALUES (:target_id, :presenter_id, :role)
            ");
            $stmt->execute([
                ':target_id' => $target_id,
                ':presenter_id' => $presenter_id,
                ':role' => $role
            ]);
            
            sendSuccess([
                'presenter_id' => $presenter_id,
                'type' => 'podcast',
                'target_id' => $target_id,
                'role' => $role
            ], 'Presenter assigned to podcast');
            
        } elseif ($type === 'media') {
            // Check if media exists
            $stmt = $pdo->prepare("SELECT id FROM media WHERE id = :id");
            $stmt->execute([':id' => $target_id]);
            if (!$stmt->fetch()) {
                sendError('Media not found', 404);
            }
            
            // Validate role
            $allowed_roles = ['host', 'guest'];
            if (!in_array($role, $allowed_roles)) {
                $role = 'guest';
            }
            
            // Check if already assigned
            $stmt = $pdo->prepare("
                SELECT id FROM media_presenters 
                WHERE media_id = :target_id AND presenter_id = :presenter_id
            ");
            $stmt->execute([
                ':target_id' => $target_id,
                ':presenter_id' => $presenter_id
            ]);
            
            if ($stmt->fetch()) {
                sendError('Presenter already assigned to this media', 400);
            }
            
            // Assign to media
            $stmt = $pdo->prepare("
                INSERT INTO media_presenters (media_id, presenter_id, role) 
                VALUES (:target_id, :presenter_id, :role)
            ");
            $stmt->execute([
                ':target_id' => $target_id,
                ':presenter_id' => $presenter_id,
                ':role' => $role
            ]);
            
            sendSuccess([
                'presenter_id' => $presenter_id,
                'type' => 'media',
                'target_id' => $target_id,
                'role' => $role
            ], 'Presenter assigned to media');
            
        } else {
            sendError('Invalid type. Must be "podcast" or "media"', 400);
        }
        
    } catch (PDOException $e) {
        sendError('Database error: ' . $e->getMessage(), 500);
    }
    
// DELETE - Remove presenter from podcast or media
} elseif ($method === 'DELETE') {
    $data = getRequestData();
    
    validateRequiredFields($data, ['presenter_id', 'type', 'target_id']);
    
    $presenter_id = $data['presenter_id'];
    $type = $data['type'];
    $target_id = $data['target_id'];
    
    try {
        if ($type === 'podcast') {
            $stmt = $pdo->prepare("
                DELETE FROM podcast_presenters 
                WHERE podcast_id = :target_id AND presenter_id = :presenter_id
            ");
        } elseif ($type === 'media') {
            $stmt = $pdo->prepare("
                DELETE FROM media_presenters 
                WHERE media_id = :target_id AND presenter_id = :presenter_id
            ");
        } else {
            sendError('Invalid type. Must be "podcast" or "media"', 400);
        }
        
        $stmt->execute([
            ':target_id' => $target_id,
            ':presenter_id' => $presenter_id
        ]);
        
        if ($stmt->rowCount() === 0) {
            sendError('Assignment not found', 404);
        }
        
        sendSuccess(null, 'Presenter removed successfully');
        
    } catch (PDOException $e) {
        sendError('Database error: ' . $e->getMessage(), 500);
    }
    
} else {
    sendError('Method not allowed', 405);
}
?>  