<script lang="ts">
  import type { PicletInstance } from '$lib/db/schema';
  import { deletePicletInstance } from '$lib/db/piclets';
  import { uiStore } from '$lib/stores/ui';
  import { TYPE_DATA } from '$lib/types/picletTypes';
  
  interface Props {
    instance: PicletInstance;
    onClose: () => void;
    onDeleted?: () => void;
  }
  
  let { instance, onClose, onDeleted }: Props = $props();
  
  const typeColor = $derived(TYPE_DATA[instance.primaryType]?.color || '#007bff');
  
  async function handleDelete() {
    if (confirm(`Are you sure you want to delete ${instance.typeId}?`)) {
      try {
        await deletePicletInstance(instance.id!);
        onDeleted?.();
        onClose();
      } catch (error) {
        console.error('Failed to delete piclet:', error);
        alert('Failed to delete piclet');
      }
    }
  }
</script>

<div class="piclet-detail-overlay" onclick={onClose}>
  <div class="piclet-detail" onclick={(e) => e.stopPropagation()}>
    <!-- Header -->
    <div class="header" style="--type-color: {typeColor}">
      <button class="close-button" onclick={onClose}>×</button>
      <div class="tier-badge tier-{instance.tier}">{instance.tier}</div>
    </div>
    
    <!-- Main Content -->
    <div class="content">
      <!-- Image Section -->
      <div class="image-section">
        <img 
          src={instance.imageData || instance.imageUrl} 
          alt={instance.typeId}
          class="piclet-image"
        />
      </div>
      
      <!-- Info Section -->
      <div class="info-section">
        <h2 class="name">{instance.typeId}</h2>
        
        <div class="types">
          <span class="type primary" style="background-color: {typeColor}">
            {instance.primaryType}
          </span>
        </div>
        
        <div class="description">
          <h3>Description</h3>
          <p>{instance.description}</p>
        </div>
        
        <div class="metadata">
          <div class="meta-item">
            <strong>Caught:</strong> 
            {instance.caught ? 'Yes' : 'No'}
          </div>
          {#if instance.caughtAt}
            <div class="meta-item">
              <strong>Caught on:</strong> 
              {new Date(instance.caughtAt).toLocaleDateString()}
            </div>
          {/if}
          {#if instance.isInRoster}
            <div class="meta-item">
              <strong>Status:</strong> 
              <span class="roster-badge">In Battle Roster</span>
            </div>
          {/if}
        </div>
      </div>
    </div>
    
    <!-- Actions -->
    <div class="actions">
      <button class="delete-button" onclick={handleDelete}>
        🗑️ Delete
      </button>
    </div>
  </div>
</div>

<style>
  .piclet-detail-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
  }
  
  .piclet-detail {
    background: white;
    border-radius: 16px;
    width: 100%;
    max-width: 400px;
    max-height: 80vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
  
  .header {
    background: var(--type-color, #007bff);
    color: white;
    padding: 1rem;
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .close-button {
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background 0.2s;
  }
  
  .close-button:hover {
    background: rgba(255, 255, 255, 0.2);
  }
  
  .tier-badge {
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: bold;
    text-transform: uppercase;
  }
  
  .tier-low { background: #6c757d; }
  .tier-medium { background: #28a745; }
  .tier-high { background: #fd7e14; }
  .tier-legendary { background: #dc3545; }
  
  .content {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
  }
  
  .image-section {
    text-align: center;
    margin-bottom: 1.5rem;
  }
  
  .piclet-image {
    width: 150px;
    height: 150px;
    object-fit: contain;
    border-radius: 12px;
    background: rgba(0, 0, 0, 0.05);
  }
  
  .info-section {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }
  
  .name {
    margin: 0;
    font-size: 1.5rem;
    font-weight: bold;
    text-align: center;
    color: var(--type-color, #007bff);
  }
  
  .types {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
  }
  
  .type {
    padding: 0.5rem 1rem;
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 600;
    text-transform: uppercase;
    color: white;
  }
  
  .type.secondary {
    background: #6c757d;
  }
  
  .description h3 {
    margin: 0 0 0.5rem 0;
    color: #333;
    font-size: 1.1rem;
  }
  
  .description p {
    margin: 0;
    line-height: 1.5;
    color: #666;
  }
  
  .metadata {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }
  
  .meta-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.9rem;
  }
  
  .roster-badge {
    background: #28a745;
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 12px;
    font-size: 0.8rem;
    font-weight: 600;
  }
  
  .actions {
    padding: 1rem 1.5rem;
    border-top: 1px solid #eee;
    display: flex;
    justify-content: center;
  }
  
  .delete-button {
    background: #dc3545;
    color: white;
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
  }
  
  .delete-button:hover {
    background: #c82333;
  }
</style>