<?php
$title = 'Homepage Deployment Center';

$deployment =
    $deployment
    ?? [];

$status =
    strtolower(
        (string)(
            $deployment['status']
            ?? 'draft'
        )
    );

$statusLabels = [

    'draft'              => 'Draft',
    'submitted'          => 'Submitted',
    'screening'          => 'Screening',
    'under_review'       => 'Under Review',
    'recommend_revision' => 'Recommend Revision',
    'recommend_accept'   => 'Recommend Accept',
    'recommend_reject'   => 'Recommend Reject',
    'revision_required'  => 'Revision Required',
    'revised'            => 'Revised',
    'accepted'           => 'Accepted',
    'production'         => 'Production',
    'galley'             => 'Galley',
    'assigned_to_issue'  => 'Assigned To Issue',
    'published'          => 'Published',
    'rejected'           => 'Rejected',
    'withdrawn'          => 'Withdrawn',
    'archived'           => 'Archived'

];

$statusClass =
    'secondary';

if ($status === 'published') {
    $statusClass = 'success';
}
elseif (
    in_array(
        $status,
        [
            'accepted',
            'production',
            'galley',
            'assigned_to_issue'
        ],
        true
    )
) {
    $statusClass = 'primary';
}
elseif (
    in_array(
        $status,
        [
            'submitted',
            'screening',
            'under_review',
            'revision_required',
            'revised',
            'recommend_revision',
            'recommend_accept',
            'recommend_reject'
        ],
        true
    )
) {
    $statusClass = 'warning';
}
elseif (
    in_array(
        $status,
        [
            'rejected',
            'withdrawn'
        ],
        true
    )
) {
    $statusClass = 'danger';
}
elseif ($status === 'archived') {
    $statusClass = 'dark';
}

$workflow = [

    'draft',
    'submitted',
    'screening',
    'under_review',
    'recommend_revision',
    'revision_required',
    'revised',
    'recommend_accept',
    'accepted',
    'production',
    'galley',
    'assigned_to_issue',
    'published'

];

$currentIndex =
    array_search(
        $status,
        $workflow,
        true
    );

if ($currentIndex === false) {
    $currentIndex = -1;
}

/*
|--------------------------------------------------------------------------
| PACK 1105 — Action Buttons
|--------------------------------------------------------------------------
| These buttons still post to the existing controller method:
| /easychair/admin/conference-home/workflow/update
|
| Controller enforcement remains the source of truth.
|--------------------------------------------------------------------------
*/

$actionsByStatus = [

    'draft' => [
        'submitted' => [
            'label' => 'Submit Homepage',
            'icon'  => 'fa-paper-plane',
            'class' => 'btn-primary'
        ],
        'withdrawn' => [
            'label' => 'Withdraw',
            'icon'  => 'fa-ban',
            'class' => 'btn-outline-danger'
        ],
    ],

    'submitted' => [
        'screening' => [
            'label' => 'Start Screening',
            'icon'  => 'fa-magnifying-glass',
            'class' => 'btn-primary'
        ],
        'withdrawn' => [
            'label' => 'Withdraw',
            'icon'  => 'fa-ban',
            'class' => 'btn-outline-danger'
        ],
    ],

    'screening' => [
        'under_review' => [
            'label' => 'Send To Review',
            'icon'  => 'fa-users-viewfinder',
            'class' => 'btn-primary'
        ],
        'rejected' => [
            'label' => 'Reject',
            'icon'  => 'fa-xmark',
            'class' => 'btn-danger'
        ],
    ],

    'under_review' => [
        'recommend_revision' => [
            'label' => 'Recommend Revision',
            'icon'  => 'fa-pen-to-square',
            'class' => 'btn-warning'
        ],
        'recommend_accept' => [
            'label' => 'Recommend Accept',
            'icon'  => 'fa-check',
            'class' => 'btn-success'
        ],
        'recommend_reject' => [
            'label' => 'Recommend Reject',
            'icon'  => 'fa-xmark',
            'class' => 'btn-danger'
        ],
    ],

    'recommend_revision' => [
        'revision_required' => [
            'label' => 'Request Revision',
            'icon'  => 'fa-rotate',
            'class' => 'btn-warning'
        ],
    ],

    'recommend_accept' => [
        'accepted' => [
            'label' => 'Accept Homepage',
            'icon'  => 'fa-circle-check',
            'class' => 'btn-success'
        ],
    ],

    'recommend_reject' => [
        'rejected' => [
            'label' => 'Reject Homepage',
            'icon'  => 'fa-circle-xmark',
            'class' => 'btn-danger'
        ],
    ],

    'revision_required' => [
        'revised' => [
            'label' => 'Mark Revised',
            'icon'  => 'fa-file-circle-check',
            'class' => 'btn-primary'
        ],
        'withdrawn' => [
            'label' => 'Withdraw',
            'icon'  => 'fa-ban',
            'class' => 'btn-outline-danger'
        ],
    ],

    'revised' => [
        'under_review' => [
            'label' => 'Return To Review',
            'icon'  => 'fa-rotate-left',
            'class' => 'btn-primary'
        ],
    ],

    'accepted' => [
        'production' => [
            'label' => 'Move To Production',
            'icon'  => 'fa-gears',
            'class' => 'btn-primary'
        ],
    ],

    'production' => [
        'galley' => [
            'label' => 'Generate Galley',
            'icon'  => 'fa-file-lines',
            'class' => 'btn-primary'
        ],
    ],

    'galley' => [
        'assigned_to_issue' => [
            'label' => 'Assign To Issue',
            'icon'  => 'fa-folder-open',
            'class' => 'btn-primary'
        ],
    ],

    'assigned_to_issue' => [
        'published' => [
            'label' => 'Publish',
            'icon'  => 'fa-rocket',
            'class' => 'btn-success'
        ],
    ],

    'published' => [
        'archived' => [
            'label' => 'Archive',
            'icon'  => 'fa-box-archive',
            'class' => 'btn-dark'
        ],
    ],

];

$availableActions =
    $actionsByStatus[$status]
    ?? [];

$currentRole =
    strtolower(
        (string)(
            $_SESSION['role_code']
            ?? $_SESSION['role']
            ?? ''
        )
    );

$isSuperAdmin =
    in_array(
        $currentRole,
        [
            'super_admin',
            'superadmin'
        ],
        true
    );

$isConferenceChair =
    (
        $currentRole ===
        'conference_chair'
    );

$isTrackChair =
    (
        $currentRole ===
        'track_chair'
    );

if ($isTrackChair) {

    $trackChairStatuses = [

        'under_review',
        'recommend_revision',
        'recommend_accept',
        'recommend_reject'

    ];

    if (
        !in_array(
            $status,
            $trackChairStatuses,
            true
        )
    ) {
        $availableActions = [];
    }
}

if (
    !$isTrackChair
    &&
    !$isConferenceChair
    &&
    !$isSuperAdmin
) {

    $availableActions = [];
}

?>

<div class="container-fluid py-4">

<div class="card border-0 shadow-lg rounded-4 mb-4">

<div class="card-body">

<h2 class="fw-bold">

<i class="fa fa-tower-broadcast text-primary"></i>

Homepage Deployment Center

</h2>

<div class="text-muted">

Manage homepage editorial workflow, deployment status, and publication readiness.

</div>

</div>

</div>

<div class="row g-4">

<div class="col-lg-4">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body text-center">

<div class="small text-muted">

Current Status

</div>

<div class="mt-3">

<span class="badge bg-<?= $statusClass ?> fs-5">

<?= htmlspecialchars(
    strtoupper(
        $statusLabels[$status]
        ?? $status
    )
) ?>

</span>

</div>

</div>

</div>

</div>

<div class="col-lg-4">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body text-center">

<div class="small text-muted">

Approved By

</div>

<div class="fw-bold mt-3">

<?= htmlspecialchars(
    $deployment['approved_by']
    ?? '-'
) ?>

</div>

</div>

</div>

</div>

<div class="col-lg-4">

<div class="card border-0 shadow-sm rounded-4 h-100">

<div class="card-body text-center">

<div class="small text-muted">

Published By

</div>

<div class="fw-bold mt-3">

<?= htmlspecialchars(
    $deployment['published_by']
    ?? '-'
) ?>

</div>

</div>

</div>

</div>

</div>

<div class="card border-0 shadow-sm rounded-4 mt-4">

<div class="card-body">

<h4 class="fw-bold mb-4">

Workflow Actions

</h4>

<span class="badge bg-dark ms-2">

<?php

if ($isSuperAdmin) {

    echo 'Super Admin';

}
elseif ($isConferenceChair) {

    echo 'Conference Chair';

}
elseif ($isTrackChair) {

    echo 'Track Chair';

}
else {

    echo 'Read Only';

}

?>

</span>

<?php if (empty($availableActions)): ?>

<div class="alert alert-info rounded-4">

No further workflow actions are available for this status.

</div>

<?php else: ?>

<div class="d-flex flex-wrap gap-2">

<?php foreach ($availableActions as $nextStatus => $action): ?>

<form
method="post"
action="/easychair/admin/conference-home/workflow/update"
onsubmit="return confirm('Move homepage workflow to <?= htmlspecialchars($statusLabels[$nextStatus] ?? $nextStatus) ?>?');">

<input
type="hidden"
name="status"
value="<?= htmlspecialchars($nextStatus) ?>">

<button
class="btn <?= htmlspecialchars($action['class']) ?> rounded-pill px-4">

<i class="fa <?= htmlspecialchars($action['icon']) ?>"></i>

<?= htmlspecialchars($action['label']) ?>

</button>

</form>

<?php endforeach; ?>

</div>

<?php endif; ?>

</div>

</div>

<div class="card border-0 shadow-sm rounded-4 mt-4">

<div class="card-body">

<h4 class="fw-bold mb-4">

Deployment Timeline

</h4>

<table class="table">

<tr>

<th>Status</th>

<td>

<?= htmlspecialchars(
    strtoupper(
        $statusLabels[$status]
        ?? $status
    )
) ?>

</td>

</tr>

<tr>

<th>Approved At</th>

<td>

<?= htmlspecialchars(
    $deployment['approved_at']
    ?? '-'
) ?>

</td>

</tr>

<tr>

<th>Published At</th>

<td>

<?= htmlspecialchars(
    $deployment['published_at']
    ?? '-'
) ?>

</td>

</tr>

<tr>

<th>Created</th>

<td>

<?= htmlspecialchars(
    $deployment['created_at']
    ?? '-'
) ?>

</td>

</tr>

</table>

</div>

</div>

<div class="card border-0 shadow-sm rounded-4 mt-4">

<div class="card-body">

<h4 class="fw-bold mb-4">

Homepage Editorial Workflow

</h4>

<div class="row g-2">

<?php foreach($workflow as $i => $step): ?>

<div class="col">

<div
class="border rounded-4 p-3 text-center
<?= $i <= $currentIndex
? 'bg-success text-white'
: '' ?>">

<?= htmlspecialchars(
$statusLabels[$step]
?? $step
) ?>

</div>

</div>

<?php endforeach; ?>

</div>

<div class="small text-muted mt-3">

Recommendation statuses are included so Track Chairs can recommend decisions while Conference Chairs retain final authority.

</div>

</div>

</div>

</div>
