<template>
  <BasicModal
    title="جزئیات کاربر"
    title-en="User details"
    width="95%"
    max-width="70rem"
    min-width="auto"
    :show-close-button="true"
  >
    <template #default="{ close }">
      <div class="user-details">
        <SkeletonLoaderBlock v-if="isLoading" :rows="4" :cols-per-row="1" />

        <template v-else-if="user">
          <div class="user-details__section">
            <div class="user-details__row user-details__row--main">
              <div class="user-details__head">
                <div class="user-details__avatar">
                  <img v-if="user.avatarUrl" :src="user.avatarUrl" :alt="fullName" />
                  <SvgIcon v-else name="user" :size="48" color="#cbd5e1" />
                </div>
                <div class="user-details__identity">
                  <div class="user-details__status" :class="statusModifier">
                    <span class="user-details__status-dot" />
                    <span>{{ user.status === 'blocked' ? 'غیر فعال' : 'فعال' }}</span>
                  </div>
                  <p class="user-details__label">نام و نام خانوادگی</p>
                  <p class="user-details__name">{{ fullName || '—' }}</p>
                  <Badge
                    class="user-details__role"
                    :variant="roleVariant"
                    label="نقش:"
                    :value="roleText"
                    dot
                  />
                </div>
              </div>
              <LineInfoBlock
                title="تاریخ تولد"
                :numeric-desc="formatJalaaliDate(user.birthday) || '—'"
              />
              <LineInfoBlock title="شماره تماس" :numeric-desc="user.phone || '—'" />
              <LineInfoBlock title="کد ملی" :numeric-desc="user.nationalCode || '—'" />
              <LineInfoBlock
                title="وضعیت تاهل"
                :desc="MARITAL_STATUS[user.marriageStatus] || '—'"
              />
              <LineInfoBlock title="جنسیت" :desc="GENDER[user.gender] || '—'" />
            </div>

            <div class="user-details__row user-details__row--location">
              <LineInfoBlock title="استان" :desc="user?.province?.name || '—'" />
              <LineInfoBlock title="شهر" :desc="user?.city?.name || '—'" />
              <LineInfoBlock title="آدرس" :desc="user?.address || '—'" />
            </div>

            <LineInfoBlock title="بیوگرافی" :desc="user.bio || '—'" />
          </div>

          <div v-if="isStudent && user.profileCompleted" class="user-details__section">
            <LineTitleBlock title="اطلاعات تحصیلی و شغلی" title-en="Academic information" />
            <div class="user-details__grid">
              <LineInfoBlock
                title="وضعیت تحصیلی"
                :desc="EDUCATION_STATUS[user.educationStatus] || '—'"
              />
              <LineInfoBlock
                title="آخرین مدرک حوزوی"
                :desc="SEMINARY_LEVEL[user.seminaryLevel] || '—'"
              />
              <LineInfoBlock
                title="آخرین مدرک دانشگاهی"
                :desc="UNIVERSITY_LEVEL[user.universityLevel] || '—'"
              />
              <LineInfoBlock title="نام حوزه/دانشگاه" :desc="user.universityName || '—'" />
            </div>
            <LineInfoBlock title="گرایش/رشته تحصیلی" :desc="user.fieldOfStudy || '—'" />
          </div>
        </template>

        <NoItems v-else title="یافت نشد" desc="اطلاعات این کاربر در دسترس نیست." />

        <div class="user-details__divider" />
        <div class="user-details__footer">
          <BaseButton text="تایید" custom-class="user-details__close-btn" @click="close">
            <template #appendIcon>
              <SvgIcon name="arrow-left" :size="18" color="#fff" />
            </template>
          </BaseButton>
        </div>
      </div>
    </template>
  </BasicModal>
</template>

<script setup>
import { computed } from 'vue'
import Badge from '@/components/Badge.vue'
import useModal from '@/composables/useModal'
import BasicModal from '@/components/BasicModal.vue'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import NoItems from '@/components/blocks/NoItems.vue'
import { formatJalaaliDate } from '@/utils/date-utils'
import LineTitleBlock from '@/components/LineTitleBlock.vue'
import { useAdminUserQuery } from '@/services/query/admin-users'
import LineInfoBlock from '@/components/blocks/LineInfoBlock.vue'
import SkeletonLoaderBlock from '@/components/blocks/SkeletonLoaderBlock.vue'
import {
  EDUCATION_STATUS,
  GENDER,
  MARITAL_STATUS,
  ROLE_LABELS,
  SEMINARY_LEVEL,
  UNIVERSITY_LEVEL,
} from '@/enums'

defineOptions({ name: 'UserDetailsModal' })

const { getModal } = useModal()

const modalData = computed(() => getModal('UserDetailsModal')?.data ?? {})
const userId = computed(() => modalData.value.id ?? null)

const { data: user, isLoading } = useAdminUserQuery(userId, {
  enabled: () => !!userId.value,
})

const fullName = computed(() => `${user.value?.name || ''}`.trim())

const roleName = computed(() => {
  const first = user.value?.roles?.[0]
  return typeof first === 'string' ? first : first?.name
})

const roleText = computed(() => {
  const name = roleName.value
  return name ? ROLE_LABELS[name] || name : 'بدون نقش'
})

const ROLE_BADGE_VARIANTS = {
  student: 'danger',
  teacher: 'warning',
  counselor: 'blue',
  missionary: 'info',
  admin: 'neutral',
}

const roleVariant = computed(() => ROLE_BADGE_VARIANTS[roleName.value] || 'neutral')

const isStudent = computed(() => roleName.value === 'student')

const statusModifier = computed(() =>
  user.value?.status === 'blocked'
    ? 'user-details__status--blocked'
    : 'user-details__status--active'
)
</script>

<style lang="scss" scoped>
.user-details {
  width: 100%;
  text-align: start;

  &__section {
    margin-bottom: 1.25rem;
  }

  &__notice {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    margin-bottom: 0.625rem;
    border-radius: 0.5rem;
    background-color: rgba(var(--color-primary-rgb, 0, 0, 0), 5%);
    color: var(--color-primary);
    font-family: var(--font-family-fa);
    font-size: 0.85rem;
  }

  &__head {
    display: flex;
    align-items: center;
    gap: 0.875rem;
  }

  &__row {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.25rem;
    margin-bottom: 0.25rem;
  }

  &__row--main {
    align-items: center;

    @media (min-width: 640px) {
      grid-template-columns: repeat(2, 1fr);
    }

    @media (min-width: 1024px) {
      grid-template-columns: auto repeat(5, 1fr);
    }
  }

  &__row--main &__head {
    grid-column: 1 / -1;

    @media (min-width: 1024px) {
      grid-column: auto;
    }
  }

  &__row--location {
    @media (min-width: 640px) {
      grid-template-columns: repeat(2, 1fr);
    }

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

  &__avatar {
    width: 5rem;
    height: 5rem;
    border-radius: 9999px;
    background: #f5f5f5;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  &__identity {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
  }

  &__status {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    font-family: var(--font-family-fa);
    font-size: 0.7rem;

    &--active {
      color: #35c245;

      .user-details__status-dot {
        background-color: #35c245;
      }
    }

    &--blocked {
      color: var(--color-error);

      .user-details__status-dot {
        background-color: var(--color-error);
      }
    }
  }

  &__status-dot {
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 9999px;
  }

  &__label {
    font-family: var(--font-family-fa);
    font-size: 0.75rem;
    color: #b7b7b7;
    margin: 0;
  }

  &__name {
    font-family: var(--font-family-fa);
    font-size: 0.95rem;
    font-weight: 500;
    color: #4b4b4b;
    margin: 0;
  }

  &__role {
    margin-top: 0.25rem;
  }

  &__grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.25rem;

    @media (min-width: 640px) {
      grid-template-columns: repeat(2, 1fr);
    }

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

  &__divider {
    border-block-end: 1px solid var(--color-thd-gray);
    margin-block: 1.25rem;
  }

  &__footer {
    display: flex;
    justify-content: flex-end;
  }

  &__close-btn {
    min-width: 10rem;
  }
}
</style>
