<?php

use App\Models\Author;

$authorModel = new Author();
$action = $_GET['action'] ?? 'list';
$id = $_GET['id'] ?? $_POST['id'] ?? null;

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = $_POST;
    
    // Handle image upload
    if (!empty($_FILES['image_upload']['name'])) {
        $uploadedImage = uploadImage($_FILES['image_upload']);
        if ($uploadedImage) {
            $data['image'] = $uploadedImage;
        }
    } else {
        // Keep current image if no new upload
        $data['image'] = $data['current_image'] ?? '';
    }
    
    // Handle social links
    if (isset($data['social_links']) && is_array($data['social_links'])) {
        // Filter out empty social links
        $socialLinks = array_filter($data['social_links'], function($link) {
            return !empty($link['platform']) && !empty($link['url']);
        });
        $data['social_links'] = array_values($socialLinks); // Reindex array
    }
    
    // Get action from form data
    $formAction = $data['action'] ?? $action;
    
    try {
        switch ($formAction) {
            case 'create':
            case 'edit':
                if ($formAction === 'create') {
                    $authorId = $authorModel->create($data);
                    $success = "Author created successfully!";
                } else {
                    $authorModel->update($id, $data);
                    $authorId = $id;
                    $success = "Author updated successfully!";
                }
                header("Location: /admin/authors/?action=edit&id=" . $authorId);
                exit;
                break;
                
            case 'delete':
                if ($id) {
                    $authorModel->delete($id);
                    $success = "Author deleted successfully!";
                    $action = 'list';
                }
                break;
        }
    } catch (Exception $e) {
        $error = "Error: " . $e->getMessage();
    }
}

// Get data for different actions
switch ($action) {
    case 'create':
    case 'new':
        $author = [
            'name' => '',
            'image' => '',
            'description' => '',
            'social_links' => []
        ];
        $action = 'create';
        break;
        
    case 'edit':
        if (!$id) {
            $action = 'list';
            break;
        }
        
        $author = $authorModel->getById($id);
        if (!$author) {
            $error = "Author not found!";
            $action = 'list';
            break;
        }
        break;
        
    case 'list':
    default:
        $authors = $authorModel->getAll();
        $action = 'list';
        break;
}

// Determine which template to use
$template = match($action) {
    'list' => 'admin/authors/list.php',
    'create', 'edit' => 'admin/authors/form.php',
    default => 'admin/authors/list.php'
};

return ViewRender($template, [
    'action' => $action,
    'authors' => $authors ?? [],
    'author' => $author ?? null,
    'success' => $success ?? null,
    'error' => $error ?? null
]);