//
//  CustomItemRow.swift
//  SportsTime
//
//  Row component for custom itinerary items with drag handle
//

import SwiftUI

struct CustomItemRow: View {
    @Environment(\.colorScheme) private var colorScheme

    let item: CustomItineraryItem
    var onTap: () -> Void
    var onDelete: () -> Void

    // Drag handle visible - users can drag to reorder using .draggable/.dropDestination

    var body: some View {
        Button(action: onTap) {
            HStack(spacing: 12) {
                // Drag handle
                Image(systemName: "line.3.horizontal")
                    .foregroundStyle(.tertiary)
                    .font(.caption)

                // Category icon
                Text(item.category.icon)
                    .font(.title3)

                // Title
                Text(item.title)
                    .font(.subheadline)
                    .foregroundStyle(Theme.textPrimary(colorScheme))
                    .lineLimit(2)

                Spacer()

                // Chevron to indicate tappable (tap to edit)
                Image(systemName: "chevron.right")
                    .foregroundStyle(.tertiary)
                    .font(.caption)
            }
            .padding(.horizontal, Theme.Spacing.md)
            .padding(.vertical, Theme.Spacing.sm)
            .background {
                // Opaque base + semi-transparent accent
                RoundedRectangle(cornerRadius: 8)
                    .fill(Theme.cardBackground(colorScheme))
                RoundedRectangle(cornerRadius: 8)
                    .fill(Theme.warmOrange.opacity(0.1))
            }
            .overlay {
                RoundedRectangle(cornerRadius: 8)
                    .strokeBorder(Theme.warmOrange.opacity(0.3), lineWidth: 1)
            }
        }
        .buttonStyle(.plain)
        .contextMenu {
            Button {
                onTap()
            } label: {
                Label("Edit", systemImage: "pencil")
            }
            Button(role: .destructive) {
                onDelete()
            } label: {
                Label("Delete", systemImage: "trash")
            }
        }
    }
}

#Preview {
    VStack {
        CustomItemRow(
            item: CustomItineraryItem(
                tripId: UUID(),
                category: .restaurant,
                title: "Joe's BBQ - Best brisket in Texas!",
                anchorDay: 1
            ),
            onTap: {},
            onDelete: {}
        )
        CustomItemRow(
            item: CustomItineraryItem(
                tripId: UUID(),
                category: .hotel,
                title: "Hilton Downtown",
                anchorDay: 1
            ),
            onTap: {},
            onDelete: {}
        )
    }
    .padding()
}
