<template>
  <div>
    <div v-if="loading" class="container py-5">
      <div class="skeleton skeleton-text w-25 mb-4"></div>
      <div class="row">
        <div class="col-lg-8">
          <div class="skeleton skeleton" style="height:380px;border-radius:12px;margin-bottom:1.5rem;"></div>
          <div class="skeleton skeleton-text w-75 mb-3"></div>
          <div class="skeleton skeleton-text-sm mb-3"></div>
          <div class="skeleton skeleton-text"></div>
          <div class="skeleton skeleton-text"></div>
          <div class="skeleton skeleton-text w-50"></div>
        </div>
      </div>
    </div>
    <div v-else-if="error" class="container py-5 text-center">
      <i class="fa-solid fa-triangle-exclamation fa-4x text-secondary mb-3" style="opacity:.4;"></i>
      <h4>Sermon not found</h4>
      <router-link to="/sermons" class="btn btn-primary mt-3">Back to Sermons</router-link>
    </div>
    <div v-else class="container py-5">
      <router-link to="/sermons" class="btn btn-outline-secondary btn-sm mb-4"><i class="fa-solid fa-arrow-left me-1"></i>Back to Sermons</router-link>
      <div class="row">
        <div class="col-lg-8 animate-up">
          <img v-if="sermon.serm_image_url" :src="sermon.serm_image_url" :alt="sermon.serm_title" class="img-fluid rounded-4 shadow mb-4 w-100" style="max-height:420px;object-fit:cover;">
          <h1 class="fw-bold">{{ sermon.serm_title }}</h1>
          <div class="d-flex flex-wrap gap-3 mb-4">
            <span v-if="sermon.serm_speaker" class="badge bg-light text-dark fs-6 fw-normal px-3 py-2"><i class="fa-solid fa-user me-1 text-primary"></i> {{ sermon.serm_speaker }}</span>
            <span v-if="sermon.serm_date" class="badge bg-light text-dark fs-6 fw-normal px-3 py-2"><i class="fa-solid fa-calendar me-1 text-primary"></i> {{ formatDate(sermon.serm_date) }}</span>
            <span v-if="sermon.serm_scripture" class="badge bg-light text-dark fs-6 fw-normal px-3 py-2"><i class="fa-solid fa-book-bible me-1 text-primary"></i> {{ sermon.serm_scripture }}</span>
          </div>
          <div class="mb-4"><p class="lead" style="white-space:pre-wrap">{{ sermon.serm_description }}</p></div>
        </div>
        <div class="col-lg-4">
          <div class="card shadow-sm border-0 sticky-top" style="top:90px;">
            <div class="card-header bg-white py-3"><h5 class="mb-0 fw-bold"><i class="fa-solid fa-headphones me-2" style="color:#0d6efd;"></i>Listen / Watch</h5></div>
            <div class="card-body">
              <template v-if="sermon.serm_audio_url">
                <h6 class="fw-bold"><i class="fa-solid fa-music me-1 text-success"></i>Audio</h6>
                <audio controls class="w-100 mb-4"><source :src="sermon.serm_audio_url" type="audio/mpeg">Your browser does not support the audio element.</audio>
              </template>
              <template v-if="sermon.serm_video_url">
                <h6 class="fw-bold"><i class="fa-solid fa-video me-1 text-danger"></i>Video</h6>
                <a :href="sermon.serm_video_url" target="_blank" class="btn btn-primary w-100 mb-3"><i class="fa-solid fa-play me-1"></i> Watch Video</a>
              </template>
              <div v-if="!sermon.serm_audio_url && !sermon.serm_video_url" class="text-center py-4 text-secondary">
                <i class="fa-solid fa-headphones fa-3x mb-2" style="opacity:.3;"></i><p class="mb-0">No media available.</p>
              </div>
              <hr><router-link to="/sermons" class="btn btn-outline-secondary w-100"><i class="fa-solid fa-list me-1"></i>All Sermons</router-link>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { getSermon } from '../api/index.js'

const route = useRoute()
const sermon = ref({})
const loading = ref(true)
const error = ref(false)

function formatDate(d) { return d ? new Date(d + 'T00:00:00').toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' }) : '' }

onMounted(async () => {
  try {
    const r = await getSermon(route.params.id)
    if (r.sermon) sermon.value = r.sermon
    else error.value = true
  } catch (e) { error.value = true }
  loading.value = false
})
</script>
