<template>
  <form class="skill-six" @submit.prevent="onSubmit">
    <div class="skill-six__grid">
      <div class="skill-six__media-col">
        <MediaRecorder
          v-model="audio"
          kind="audio"
          sub-type="audio"
          context="verification"
          purpose="voice"
          :max-seconds="180"
          :error="errors.faithProductionAudio"
        />
      </div>
      <div class="skill-six__content-col">
        <LineTitleBlock title="مهارت‌ها و تخصص‌ها" title-en="Skills & Expertise" />
        <p class="skill-six__intro">
          لطفا ویدئو را مشاهده نمایید و در قالب صوت نظرات و تحلیل خود را بیان کرده و بفرمایید چگونه
          می‌توان از سرمایه ایمان الهی، عمل صالح تولید نمود؟
        </p>
        <div v-if="videoUrl" class="skill-six__video-wrap">
          <video :src="videoUrl" controls playsinline class="skill-six__video" />
        </div>
        <div v-else class="skill-six__video-placeholder">
          <p>ویدیوی راهنما هنوز بارگذاری نشده است.</p>
          <p>تا زمان انتشار ویدیو، ارسال پاسخ در این مرحله اختیاری است.</p>
        </div>
      </div>
    </div>

    <div class="skill-six__divider" />
    <StepActions :loading="saveRegisterMutation.isPending.value" @back="store.goToPrevStep()" />
  </form>
</template>

<script setup>
import useYup from '@/composables/useYup'
import { computed, ref, watch } from 'vue'
import { STUDENT_REGISTRATION } from '@/enums'
import LineTitleBlock from '@/components/LineTitleBlock.vue'
import { useSaveRegisterDataMutation } from '@/services/query/auth'
import StepActions from '@/features/auth/components/studentRegister/StepActions.vue'
import { skillAssessmentPart6Schema } from '@/features/auth/schema/student-register'
import MediaRecorder from '@/features/auth/components/studentRegister/MediaRecorder.vue'
import { useStudentRegistrationStore } from '@/features/auth/store/student-registration'

const store = useStudentRegistrationStore()

const audio = ref(store.skill6.faithProductionAudio || null)

const form = ref({
  faithProductionAudio: store.skill6.faithProductionAudio || null,
  faithProductionId: store.skill6.faithProductionId || '',
})

const { validate, errors } = useYup(skillAssessmentPart6Schema)

watch(audio, (value) => {
  form.value.faithProductionAudio = value
  form.value.faithProductionId = value?.uploadId || ''
})

watch(form, (value) => store.updateSection('skill6', value), { deep: true })

const videoUrl = computed(() => store.registrationMedia.faithProductionVideoUrl || '')

const saveRegisterMutation = useSaveRegisterDataMutation()

const onSubmit = async () => {
  const { isValid, payload } = await validate(form.value)
  if (!isValid) return
  await saveRegisterMutation.mutateAsync(payload)
  store.skill6 = { ...store.skill6, ...payload }
  store.markStepAsCompleted(STUDENT_REGISTRATION.SKILL_ASSESSMENT_PART_6)
  store.goToNextStep()
}
</script>

<style lang="scss" scoped>
.skill-six {
  &__grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;

    @media (min-width: 1024px) {
      grid-template-columns: 3fr 9fr;
    }
  }

  &__media-col {
    // Recorder/uploader stays first: top of the stack on mobile, left on desktop.
    order: 1;

    @media (min-width: 1024px) {
      padding: 0.5rem;
    }
  }

  &__content-col {
    order: 2;

    @media (min-width: 1024px) {
      padding-inline-start: 1.25rem;
    }
  }

  &__intro {
    font-family: var(--font-family-fa);
    font-weight: 400;
    font-size: 0.9375rem;
    line-height: 1.7;
    margin: 0 0 0.75rem;
    text-align: justify;
  }

  &__video-wrap {
    width: 100%;
    border-radius: 1rem;
    overflow: hidden;
    background: #000;
  }

  &__video {
    width: 100%;
    height: auto;
    display: block;
  }

  &__video-placeholder {
    border-radius: 1rem;
    background: #f3f4f6;
    color: #666;
    padding: 2rem;
    text-align: center;
    font-family: var(--font-family-fa);
    font-size: 0.9375rem;
    font-weight: 400;

    p + p {
      margin-top: 0.5rem;
    }
  }

  &__divider {
    border-block-end: 1px solid var(--color-thd-gray);
    margin: 1rem 0 1.5rem;
  }
}
</style>
