<script setup lang="ts">
import { computed, ref } from "vue";
import type {
  AssociatedProfileSortKey,
  AssociatedProfileSummary,
  BookmarkAssociatedProfileSummary,
} from "../../types/browser";
import { profileAvatarSrc } from "../../utils/icons";
import { sortAssociatedProfiles } from "../../utils/sort";

type ModalProfile = AssociatedProfileSummary | BookmarkAssociatedProfileSummary;

const props = defineProps<{
  title: string;
  profiles: ModalProfile[];
  browserId: string;
  browserFamilyId: string | null;
  isBookmark: boolean;
  isOpeningProfile: (browserId: string, profileId: string) => boolean;
}>();

const emit = defineEmits<{
  close: [];
  openProfile: [browserId: string, profileId: string];
}>();

const sortKey = ref<AssociatedProfileSortKey>("id");
const sortedProfiles = computed(() => sortAssociatedProfiles(props.profiles, sortKey.value));

function hasBookmarkPath(profile: ModalProfile): profile is BookmarkAssociatedProfileSummary {
  return "bookmarkPath" in profile;
}
</script>

<template>
  <div class="modal-backdrop" @click.self="emit('close')">
    <section class="modal-card">
      <div class="modal-header">
        <h3>{{ title }}</h3>
        <button class="secondary-button modal-close-button" type="button" @click="emit('close')">
          Close
        </button>
      </div>

      <div class="modal-table">
        <div class="modal-table-header modal-grid" :class="{ bookmark: isBookmark }">
          <div class="header-cell icon-cell">Avatar</div>
          <button class="header-cell sortable" :class="{ active: sortKey === 'name' }" type="button" @click="sortKey = 'name'">Name</button>
          <button class="header-cell sortable" :class="{ active: sortKey === 'id' }" type="button" @click="sortKey = 'id'">Profile ID</button>
          <div v-if="isBookmark" class="header-cell">Bookmark Path</div>
          <div class="header-cell actions-cell">Action</div>
        </div>

        <div class="modal-list">
          <article
            v-for="profile in sortedProfiles"
            :key="profile.id"
            class="modal-table-row modal-grid"
            :class="{ bookmark: isBookmark }"
          >
            <div class="modal-profile-avatar">
              <img
                v-if="profileAvatarSrc(profile, browserFamilyId)"
                :src="profileAvatarSrc(profile, browserFamilyId) ?? undefined"
                :alt="`${profile.name} avatar`"
              />
              <span v-else>{{ profile.avatarLabel }}</span>
            </div>
            <div class="row-cell primary-cell">
              <strong>{{ profile.name }}</strong>
            </div>
            <div class="row-cell">
              <span class="badge neutral">{{ profile.id }}</span>
            </div>
            <div
              v-if="isBookmark && hasBookmarkPath(profile)"
              class="row-cell muted-cell"
              :title="profile.bookmarkPath"
            >
              {{ profile.bookmarkPath }}
            </div>
            <div class="row-cell actions-cell">
              <button
                class="card-action-button"
                type="button"
                :disabled="isOpeningProfile(browserId, profile.id)"
                @click="emit('openProfile', browserId, profile.id)"
              >
                {{ isOpeningProfile(browserId, profile.id) ? "Opening..." : "Open" }}
              </button>
            </div>
          </article>
        </div>
      </div>
    </section>
  </div>
</template>

<style scoped>
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(15, 23, 42, 0.26);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.modal-card {
  width: min(760px, 100%);
  max-height: min(78vh, 820px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  border: 1px solid var(--panel-border);
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 28px 70px rgba(15, 23, 42, 0.18);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal-header h3 {
  margin: 0;
  font-weight: 600;
  letter-spacing: -0.03em;
}

.modal-table {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: 18px;
  background: var(--panel-strong);
  overflow: hidden;
}

.modal-grid {
  display: grid;
  grid-template-columns: 56px minmax(180px, 1fr) 132px 110px;
  gap: 12px;
  align-items: center;
}

.modal-grid.bookmark {
  grid-template-columns: 56px minmax(140px, 0.9fr) 120px minmax(180px, 1fr) 110px;
}

.modal-table-header {
  padding: 10px 14px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.14);
  background: rgba(248, 250, 252, 0.82);
}

.header-cell {
  color: var(--muted);
  font-size: 0.81rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.header-cell.sortable {
  padding: 0;
  text-align: left;
  background: transparent;
  cursor: pointer;
}

.header-cell.sortable.active {
  color: var(--text);
}

.modal-list {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: auto;
}

.modal-table-row {
  padding: 12px 14px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.12);
}

.modal-table-row:last-child {
  border-bottom: 0;
}

.modal-table-row:hover {
  background: rgba(248, 250, 252, 0.65);
}

.modal-profile-avatar {
  display: grid;
  place-items: center;
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  background: linear-gradient(135deg, #dbeafe, #eff6ff);
  color: #1d4ed8;
  font-size: 0.96rem;
  font-weight: 700;
  overflow: hidden;
}

.modal-profile-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.row-cell {
  min-width: 0;
}

.primary-cell strong {
  display: block;
  font-size: 0.93rem;
  line-height: 1.3;
}

.muted-cell {
  color: var(--muted);
  font-size: 0.86rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.actions-cell {
  display: flex;
  justify-content: flex-end;
}

.icon-cell {
  padding-left: 4px;
}

@media (max-width: 720px) {
  .modal-backdrop {
    padding: 12px;
  }

  .modal-grid,
  .modal-grid.bookmark {
    grid-template-columns: 56px minmax(0, 1fr) 96px;
  }

  .modal-grid > :nth-child(4),
  .modal-grid.bookmark > :nth-child(4) {
    display: none;
  }
}
</style>
