<template>
  <div class="status-timeline-container">
    <!-- Timeline Tab -->
    <div class="timeline-section">
      <h3>📊 Order Status Timeline</h3>
      <div class="timeline">
        <div v-for="(step, index) in statusHistory" :key="index" 
             class="timeline-item" :class="{ active: step.is_current, completed: step.is_completed }">
          <div class="timeline-connector" v-if="index < statusHistory.length - 1"></div>
          <div class="timeline-marker">
            <span class="status-icon">{{ step.icon }}</span>
          </div>
          <div class="timeline-content">
            <div class="status-header">
              <h4 class="status-title">{{ step.title }}</h4>
              <span class="status-badge" :class="step.status">{{ formatStatus(step.status) }}</span>
            </div>
            <div class="status-details">
              <div class="timestamp">
                <span class="time-icon">🕐</span>
                {{ formatDateTime(step.timestamp) }}
              </div>
              <div class="changed-by">
                <span class="user-icon">👤</span>
                <span class="role-badge" :class="step.changed_by.role">{{ step.changed_by.role }}</span>
                {{ step.changed_by.name }}
              </div>
              <div v-if="step.reason" class="reason">
                <span class="reason-icon">📝</span>
                {{ step.reason }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Audit Log Tab -->
    <div class="audit-section">
      <h3>📋 Audit Log</h3>
      <div class="audit-table">
        <div class="audit-header">
          <div class="col-action">Action</div>
          <div class="col-user">Performed By</div>
          <div class="col-notes">Notes</div>
          <div class="col-time">Time</div>
        </div>
        <div class="audit-body">
          <div v-for="log in auditLogs" :key="log.id" class="audit-row">
            <div class="col-action">
              <div class="action-info">
                <span class="action-icon">{{ log.action_icon }}</span>
                <span class="action-text">{{ log.action }}</span>
              </div>
            </div>
            <div class="col-user">
              <div class="user-info">
                <span class="role-badge" :class="log.user.role">{{ log.user.role }}</span>
                <span class="user-name">{{ log.user.name }}</span>
              </div>
            </div>
            <div class="col-notes">
              <span class="notes-text">{{ log.notes || '-' }}</span>
            </div>
            <div class="col-time">
              <div class="time-info">
                <span class="date">{{ formatDate(log.created_at) }}</span>
                <span class="time">{{ formatTime(log.created_at) }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue'

export default {
  name: 'OrderStatusTimeline',
  props: {
    order: {
      type: Object,
      required: true
    }
  },
  setup(props) {
    // Mock status history data
    const statusHistory = ref([
      {
        status: 'pending',
        title: 'Order Placed',
        icon: '📝',
        timestamp: '2024-01-15T10:30:00Z',
        changed_by: { role: 'customer', name: 'John Doe' },
        reason: null,
        is_completed: true,
        is_current: false
      },
      {
        status: 'accepted',
        title: 'Order Accepted',
        icon: '✅',
        timestamp: '2024-01-15T10:35:00Z',
        changed_by: { role: 'manager', name: 'Sarah Wilson' },
        reason: null,
        is_completed: true,
        is_current: false
      },
      {
        status: 'preparing',
        title: 'Preparing Food',
        icon: '👨‍🍳',
        timestamp: '2024-01-15T10:40:00Z',
        changed_by: { role: 'cashier', name: 'Mike Chen' },
        reason: null,
        is_completed: true,
        is_current: false
      },
      {
        status: 'ready_for_pickup',
        title: 'Ready for Pickup',
        icon: '📦',
        timestamp: '2024-01-15T11:10:00Z',
        changed_by: { role: 'cashier', name: 'Mike Chen' },
        reason: null,
        is_completed: true,
        is_current: false
      },
      {
        status: 'picked_up',
        title: 'Picked Up',
        icon: '🚴',
        timestamp: '2024-01-15T11:15:00Z',
        changed_by: { role: 'delivery-boy', name: 'Alex Kumar' },
        reason: null,
        is_completed: false,
        is_current: true
      },
      {
        status: 'delivered',
        title: 'Delivered',
        icon: '🎉',
        timestamp: null,
        changed_by: null,
        reason: null,
        is_completed: false,
        is_current: false
      }
    ])

    // Mock audit log data
    const auditLogs = ref([
      {
        id: 1,
        action: 'Status Changed',
        action_icon: '🔄',
        user: { role: 'super-admin', name: 'Admin User' },
        notes: 'Manually updated status to preparing due to kitchen delay',
        created_at: '2024-01-15T10:40:00Z'
      },
      {
        id: 2,
        action: 'Delivery Boy Assigned',
        action_icon: '👨‍💼',
        user: { role: 'manager', name: 'Sarah Wilson' },
        notes: 'Assigned Alex Kumar for delivery',
        created_at: '2024-01-15T11:00:00Z'
      },
      {
        id: 3,
        action: 'Payment Verified',
        action_icon: '💳',
        user: { role: 'cashier', name: 'Mike Chen' },
        notes: 'Payment confirmed via card',
        created_at: '2024-01-15T10:32:00Z'
      },
      {
        id: 4,
        action: 'Customer Notification Sent',
        action_icon: '📱',
        user: { role: 'system', name: 'System' },
        notes: 'SMS sent to customer about order status',
        created_at: '2024-01-15T11:15:00Z'
      },
      {
        id: 5,
        action: 'Order Created',
        action_icon: '📝',
        user: { role: 'customer', name: 'John Doe' },
        notes: 'Order placed through mobile app',
        created_at: '2024-01-15T10:30:00Z'
      }
    ])

    const formatStatus = (status) => {
      return status.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase())
    }

    const formatDateTime = (dateString) => {
      if (!dateString) return 'Pending'
      const date = new Date(dateString)
      return date.toLocaleString('en-US', {
        month: 'short',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit'
      })
    }

    const formatDate = (dateString) => {
      const date = new Date(dateString)
      return date.toLocaleDateString('en-US', {
        month: 'short',
        day: 'numeric',
        year: 'numeric'
      })
    }

    const formatTime = (dateString) => {
      const date = new Date(dateString)
      return date.toLocaleTimeString('en-US', {
        hour: '2-digit',
        minute: '2-digit'
      })
    }

    return {
      statusHistory,
      auditLogs,
      formatStatus,
      formatDateTime,
      formatDate,
      formatTime
    }
  }
}
</script>

<style scoped>
.status-timeline-container {
  display: flex;
  flex-direction: column;
  gap: 3rem;
}

.timeline-section, .audit-section {
  background: rgba(15, 23, 42, 0.8);
  border-radius: 20px;
  border: 1px solid rgba(59, 130, 246, 0.2);
  backdrop-filter: blur(20px);
  padding: 2rem;
}

.timeline-section h3, .audit-section h3 {
  margin: 0 0 2rem 0;
  color: #f8fafc;
  font-size: 1.5rem;
  font-weight: 700;
}

.timeline {
  position: relative;
  padding-left: 2rem;
}

.timeline-item {
  position: relative;
  margin-bottom: 2rem;
  display: flex;
  align-items: flex-start;
  gap: 1.5rem;
}

.timeline-connector {
  position: absolute;
  left: -1.75rem;
  top: 3rem;
  width: 2px;
  height: calc(100% + 1rem);
  background: rgba(59, 130, 246, 0.3);
}

.timeline-item.completed .timeline-connector {
  background: rgba(16, 185, 129, 0.5);
}

.timeline-marker {
  position: relative;
  z-index: 2;
  margin-left: -2.5rem;
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(30, 41, 59, 0.8);
  border: 3px solid rgba(59, 130, 246, 0.3);
  flex-shrink: 0;
}

.timeline-item.completed .timeline-marker {
  background: rgba(16, 185, 129, 0.2);
  border-color: #10b981;
}

.timeline-item.active .timeline-marker {
  background: rgba(59, 130, 246, 0.2);
  border-color: #3b82f6;
  animation: pulse 2s infinite;
}

.status-icon {
  font-size: 1.25rem;
}

.timeline-content {
  flex: 1;
  background: rgba(30, 41, 59, 0.6);
  border-radius: 16px;
  padding: 1.5rem;
  border: 1px solid rgba(59, 130, 246, 0.2);
}

.status-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.status-title {
  margin: 0;
  color: #f8fafc;
  font-size: 1.125rem;
  font-weight: 600;
}

.status-badge {
  padding: 0.25rem 0.75rem;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
}

.status-badge.pending { background: rgba(245, 158, 11, 0.2); color: #f59e0b; }
.status-badge.accepted { background: rgba(59, 130, 246, 0.2); color: #3b82f6; }
.status-badge.preparing { background: rgba(139, 92, 246, 0.2); color: #8b5cf6; }
.status-badge.ready_for_pickup { background: rgba(16, 185, 129, 0.2); color: #10b981; }
.status-badge.picked_up { background: rgba(99, 102, 241, 0.2); color: #6366f1; }
.status-badge.delivered { background: rgba(34, 197, 94, 0.2); color: #22c55e; }

.status-details {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.timestamp, .changed-by, .reason {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #cbd5e1;
  font-size: 0.875rem;
}

.time-icon, .user-icon, .reason-icon {
  font-size: 1rem;
}

.role-badge {
  padding: 0.125rem 0.5rem;
  border-radius: 8px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
}

.role-badge.customer { background: rgba(59, 130, 246, 0.2); color: #3b82f6; }
.role-badge.manager { background: rgba(139, 92, 246, 0.2); color: #8b5cf6; }
.role-badge.cashier { background: rgba(16, 185, 129, 0.2); color: #10b981; }
.role-badge.delivery-boy { background: rgba(245, 158, 11, 0.2); color: #f59e0b; }
.role-badge.super-admin { background: rgba(239, 68, 68, 0.2); color: #ef4444; }
.role-badge.system { background: rgba(107, 114, 128, 0.2); color: #6b7280; }

.audit-table {
  background: rgba(30, 41, 59, 0.6);
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid rgba(59, 130, 246, 0.2);
}

.audit-header {
  display: grid;
  grid-template-columns: 2fr 2fr 3fr 1.5fr;
  gap: 1rem;
  padding: 1rem 1.5rem;
  background: rgba(59, 130, 246, 0.1);
  border-bottom: 1px solid rgba(59, 130, 246, 0.2);
  font-weight: 600;
  color: #f8fafc;
  font-size: 0.875rem;
}

.audit-body {
  max-height: 400px;
  overflow-y: auto;
}

.audit-row {
  display: grid;
  grid-template-columns: 2fr 2fr 3fr 1.5fr;
  gap: 1rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid rgba(59, 130, 246, 0.1);
  transition: background 0.3s ease;
}

.audit-row:hover {
  background: rgba(59, 130, 246, 0.05);
}

.audit-row:last-child {
  border-bottom: none;
}

.action-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.action-icon {
  font-size: 1rem;
}

.action-text {
  color: #e2e8f0;
  font-weight: 500;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.user-name {
  color: #cbd5e1;
  font-size: 0.875rem;
}

.notes-text {
  color: #94a3b8;
  font-size: 0.875rem;
  line-height: 1.4;
}

.time-info {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.date {
  color: #e2e8f0;
  font-size: 0.875rem;
  font-weight: 500;
}

.time {
  color: #94a3b8;
  font-size: 0.75rem;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.4); }
  50% { box-shadow: 0 0 0 10px rgba(59, 130, 246, 0); }
}

@media (max-width: 768px) {
  .status-timeline-container {
    gap: 2rem;
  }
  
  .timeline-section, .audit-section {
    padding: 1.5rem;
  }
  
  .timeline {
    padding-left: 1rem;
  }
  
  .timeline-marker {
    margin-left: -1.75rem;
    width: 2.5rem;
    height: 2.5rem;
  }
  
  .status-icon {
    font-size: 1rem;
  }
  
  .timeline-content {
    padding: 1rem;
  }
  
  .status-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }
  
  .audit-header, .audit-row {
    grid-template-columns: 1fr;
    gap: 0.5rem;
  }
  
  .audit-header {
    display: none;
  }
  
  .audit-row {
    padding: 1rem;
    border-radius: 12px;
    margin-bottom: 0.5rem;
    background: rgba(59, 130, 246, 0.05);
  }
  
  .col-action, .col-user, .col-notes, .col-time {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
  }
  
  .col-action::before { content: "Action:"; font-weight: 600; color: #94a3b8; font-size: 0.75rem; }
  .col-user::before { content: "Performed By:"; font-weight: 600; color: #94a3b8; font-size: 0.75rem; }
  .col-notes::before { content: "Notes:"; font-weight: 600; color: #94a3b8; font-size: 0.75rem; }
  .col-time::before { content: "Time:"; font-weight: 600; color: #94a3b8; font-size: 0.75rem; }
}
</style>