<?php
if (!defined('ABSPATH')) {
    exit;
}

use BLR\Backline\Core\Media;

$base = rtrim((string) ($ctx['base_url'] ?? ''), '/') . '/';
$id   = (int) ($ctx['id'] ?? 0);
$post = $id ? get_post($id) : null;

if (!$post || $post->post_type !== 'blr_release') {
    echo '<div class="container-fluid p-0"><div class="alert alert-danger">' .
        esc_html__('Album not found.', 'blr-backline') .
        '</div></div>';
    return;
}

$edit_url   = $base . 'albums/edit/' . (int) $id . '/';
$delete_url = $base . 'albums/delete/' . (int) $id . '/';

$delete_item_label = sprintf(
    /* translators: %s: album title. */
    __('album “%s”', 'blr-backline'),
    get_the_title($post)
);

$release_date  = (string) get_post_meta($id, 'blr_release_date', true);
$type_terms    = wp_get_object_terms($id, 'blr_release_type', ['fields' => 'names']);
$release_type  = (!is_wp_error($type_terms) && !empty($type_terms)) ? (string) $type_terms[0] : '';
$artist        = (string) get_post_meta($id, 'blr_artist', true);
$upc           = (string) get_post_meta($id, 'blr_upc', true);
$release_notes = (string) get_post_meta($id, 'blr_release_notes', true);

$status_terms   = wp_get_object_terms($id, 'blr_release_status', ['fields' => 'names']);
$release_status = (!is_wp_error($status_terms) && !empty($status_terms)) ? (string) $status_terms[0] : '';

$platform_links = array_filter([
    __('Spotify', 'blr-backline')     => (string) get_post_meta($id, 'blr_spotify_url', true),
    __('Apple Music', 'blr-backline') => (string) get_post_meta($id, 'blr_apple_music_url', true),
    __('Bandcamp', 'blr-backline')    => (string) get_post_meta($id, 'blr_bandcamp_url', true),
    __('YouTube', 'blr-backline')     => (string) get_post_meta($id, 'blr_youtube_url', true),
    __('SoundCloud', 'blr-backline')  => (string) get_post_meta($id, 'blr_soundcloud_url', true),
]);

$track_ids = array_values(
    array_filter(
        array_map('absint', (array) get_post_meta($id, 'blr_track_ids', true))
    )
);

$player_tracks = [];

foreach ($track_ids as $tid) {
    $track_post = get_post($tid);

    if (!$track_post || $track_post->post_type !== 'blr_track') {
        continue;
    }

    $player_tracks[] = [
        'id'            => $tid,
        'title'         => (string) $track_post->post_title,
        'display_title' => (string) $track_post->post_title,
        'track_number'  => (string) get_post_meta($tid, 'blr_track_number', true),
        'artist'        => (string) get_post_meta($tid, 'blr_artist', true) ?: $artist,
        'audio_url'     => (string) get_post_meta($tid, 'blr_audio_url', true),
        'duration'      => (string) get_post_meta($tid, 'blr_duration', true),
        'view_url'      => $base . 'tracks/view/' . (int) $tid . '/',
    ];
}

$player_variant       = 'full';
$player_title         = (string) get_the_title($post);
$player_artist        = $artist;
$player_cover_url     = (string) wp_get_attachment_image_url(get_post_thumbnail_id($id), 'large');
$player_show_playlist = true;
$player_empty_message = __('No tracks attached to this album yet.', 'blr-backline');
?>

<div class="container-fluid p-0">
    <div class="d-flex align-items-center justify-content-between mb-3">
        <div>
            <h1 class="h3 mb-1">
                <?php echo esc_html(get_the_title($post)); ?>
            </h1>
            <div class="text-muted">
                <?php
                echo esc_html(
                    implode(
                        ' • ',
                        array_filter([$artist, $release_type, $release_status, $release_date])
                    ) ?: '—'
                );
                ?>
            </div>
        </div>

        <div class="d-flex gap-2">
            <a class="btn btn-outline-secondary" href="<?php echo esc_url($base . 'albums/'); ?>">
                <?php echo esc_html__('Back', 'blr-backline'); ?>
            </a>

            <a class="btn btn-primary" href="<?php echo esc_url($edit_url); ?>">
                <?php echo esc_html__('Edit', 'blr-backline'); ?>
            </a>

            <form
                method="post"
                action="<?php echo esc_url($delete_url); ?>"
                class="blr-ba-delete-form d-inline"
                data-item-label="<?php echo esc_attr($delete_item_label); ?>"
            >
                <?php wp_nonce_field('blr_ba_delete_blr_release_' . (int) $id); ?>

                <button type="submit" class="btn btn-outline-danger">
                    <?php echo esc_html__('Delete', 'blr-backline'); ?>
                </button>
            </form>
        </div>
    </div>

    <div class="row g-3">
        <div class="col-12 col-lg-4">
            <div class="card">
                <div class="card-body">
                    <?php echo wp_kses_post(Media::preview_markup(absint(get_post_thumbnail_id($id)), 'large')); ?>
                </div>
            </div>

            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <?php echo esc_html__('Release Info', 'blr-backline'); ?>
                    </h5>
                </div>

                <div class="card-body">
                    <dl class="row mb-0">
                        <dt class="col-5">
                            <?php echo esc_html__('Status', 'blr-backline'); ?>
                        </dt>
                        <dd class="col-7">
                            <?php echo esc_html($release_status ?: '—'); ?>
                        </dd>

                        <dt class="col-5">
                            <?php echo esc_html__('Type', 'blr-backline'); ?>
                        </dt>
                        <dd class="col-7">
                            <?php echo esc_html($release_type ?: '—'); ?>
                        </dd>

                        <dt class="col-5">
                            <?php echo esc_html__('Release Date', 'blr-backline'); ?>
                        </dt>
                        <dd class="col-7">
                            <?php echo esc_html($release_date ?: '—'); ?>
                        </dd>

                        <dt class="col-5">
                            <?php echo esc_html__('Artist', 'blr-backline'); ?>
                        </dt>
                        <dd class="col-7">
                            <?php echo esc_html($artist ?: '—'); ?>
                        </dd>

                        <dt class="col-5">
                            <?php echo esc_html__('UPC', 'blr-backline'); ?>
                        </dt>
                        <dd class="col-7">
                            <?php echo esc_html($upc ?: '—'); ?>
                        </dd>
                    </dl>

                    <?php if ($platform_links) : ?>
                        <hr>
                        <div class="d-flex flex-wrap gap-2">
                            <?php foreach ($platform_links as $label => $url) : ?>
                                <a
                                    class="btn btn-sm btn-outline-secondary"
                                    href="<?php echo esc_url($url); ?>"
                                    target="_blank"
                                    rel="noopener"
                                >
                                    <?php echo esc_html($label); ?>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>

                    <?php if (trim($release_notes) !== '') : ?>
                        <hr>
                        <div class="small text-muted mb-1">
                            <?php echo esc_html__('Release Notes', 'blr-backline'); ?>
                        </div>
                        <div>
                            <?php echo wp_kses_post(wpautop($release_notes)); ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <div class="col-12 col-lg-8">
            <div class="cardx">
                <div class="card-body">
                    <?php include BLR_BA_DIR . 'app/Views/partials/audio-player.php'; ?>
                </div>
            </div>

            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <?php echo esc_html__('Description', 'blr-backline'); ?>
                    </h5>
                </div>

                <div class="card-body">
                    <?php
                    echo trim((string) $post->post_content) !== ''
                        ? wp_kses_post(wpautop($post->post_content))
                        : '<div class="text-muted">' . esc_html__('No description yet.', 'blr-backline') . '</div>';
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>
<p class="small text-muted mb-0"><?php printf(/* translators: 1: modified date, 2: last editor display name */ esc_html__('Last edited %1$s by %2$s', 'blr-backline'), esc_html(get_the_modified_date(get_option('date_format') . ' ' . get_option('time_format'))), esc_html(($r = wp_get_post_revisions(get_the_ID(), ['numberposts' => 1])) ? get_the_author_meta('display_name', (int) array_shift($r)->post_author) : get_the_author_meta('display_name', (int) get_post_field('post_author', get_the_ID())))); ?></p>