<?php

require_once __DIR__ . '/../app/auth.php';
require_once __DIR__ . '/../app/layout.php';
require_once __DIR__ . '/../app/helpers.php';
require_once __DIR__ . '/../app/marketing.php';

$user = sparrow_require_backyard_login();

$notice = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';

    if ($action === 'delete_marketing_post') {
        $post_id = (int) ($_POST['post_id'] ?? 0);

        try {
            if ($post_id > 0) {
                sparrow_delete_marketing_post($post_id);
                $notice = 'Marketing post deleted.';
            }
        } catch (Throwable $e) {
            $error = 'Marketing post could not be deleted.';
        }
    }
}

$posts = sparrow_get_marketing_posts();

sparrow_backyard_header('Marketing Posts');

?>

<?php if ($notice): ?>
    <div class="notice-success">
        <?php echo sparrow_escape($notice); ?>
    </div>
<?php endif; ?>

<?php if ($error): ?>
    <div class="notice-error">
        <?php echo sparrow_escape($error); ?>
    </div>
<?php endif; ?>

<section class="card">
    <div class="page-heading-row">
        <div>
            <h1>Marketing Posts</h1>
            <p class="muted">
                Draft captions for Facebook and Instagram.
            </p>
        </div>

        <div class="heading-actions">
            <a class="button" href="<?php echo sparrow_escape(sparrow_url('/backyard/marketing-edit.php')); ?>">
                Add Marketing Post
            </a>
        </div>
    </div>
</section>

<section class="card">
    <h2>Social Post Drafts</h2>

    <?php if (!$posts): ?>
        <p class="muted">
            No marketing posts yet. Add your first Facebook or Instagram caption draft.
        </p>
    <?php else: ?>
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Image</th>
                        <th>Title</th>
                        <th>Platforms</th>
                        <th>Status</th>
                        <th>Scheduled</th>
                        <th>Created</th>
                        <th>Actions</th>
                    </tr>
                </thead>

                <tbody>
                    <?php foreach ($posts as $post): ?>
                        <tr>
                            <td>
                                <?php if (!empty($post['image_path'])): ?>
                                    <img
                                        class="marketing-admin-thumb"
                                        src="<?php echo sparrow_escape(sparrow_url($post['image_path'])); ?>"
                                        alt="<?php echo sparrow_escape($post['title']); ?>"
                                    >
                                <?php else: ?>
                                    <span class="marketing-empty-thumb">No image</span>
                                <?php endif; ?>
                            </td>

                            <td>
                                <strong><?php echo sparrow_escape($post['title']); ?></strong>

                                <?php if (!empty($post['message'])): ?>
                                    <br>
                                    <span class="muted">
                                        <?php echo sparrow_escape(mb_strimwidth($post['message'], 0, 90, '...')); ?>
                                    </span>
                                <?php endif; ?>
                            </td>

                            <td>
                                <?php if ((int) $post['platform_facebook'] === 1): ?>
                                    <span class="marketing-pill">Facebook</span>
                                <?php endif; ?>

                                <?php if ((int) $post['platform_instagram'] === 1): ?>
                                    <span class="marketing-pill">Instagram</span>
                                <?php endif; ?>
                            </td>

                            <td>
                                <span class="marketing-pill marketing-pill-status">
                                    <?php echo sparrow_escape($post['status']); ?>
                                </span>
                            </td>

                            <td>
                                <?php echo !empty($post['scheduled_at']) ? sparrow_escape($post['scheduled_at']) : '<span class="muted">Not scheduled</span>'; ?>
                            </td>

                            <td>
                                <?php echo sparrow_escape($post['created_at']); ?>
                            </td>

                            <td>
                                <a class="button button-secondary" href="<?php echo sparrow_escape(sparrow_url('/backyard/marketing-edit.php?id=' . (int) $post['id'])); ?>">
                                    Edit
                                </a>

                                <form method="post" style="display:inline;" onsubmit="return confirm('Delete this marketing post?');">
                                    <input type="hidden" name="action" value="delete_marketing_post">
                                    <input type="hidden" name="post_id" value="<?php echo (int) $post['id']; ?>">
                                    <button type="submit" class="button button-secondary">
                                        Delete
                                    </button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>

<?php sparrow_backyard_footer(); ?>