<script lang="ts">
    import { cart, cartItems, cartIsOpen } from '$lib/stores/cart';
    import { fly } from 'svelte/transition';
    import { quintOut } from 'svelte/easing';
    import { region } from '$lib/stores/cart';

    function calculateItemTotal(item: any) {
        return (item.unit_price * item.quantity).toFixed(2);
    }

    function calculateTotal(items: any[]) {
        return items.reduce((sum, item) => 
            sum + (item.unit_price * item.quantity), 0
        ).toFixed(2);
    }

    function getCurrencySymbol(currency_code: string) {
        switch (currency_code?.toLowerCase()) {
            case 'eur':
                return '€';
            case 'usd':
                return '$';
            case 'kwd':
                return 'KWD';
            default:
                return currency_code?.toUpperCase() || '';
        }
    }

    let currencyCode = $region?.currency_code || 'KWD';
    let currencySymbol = getCurrencySymbol(currencyCode);
    console.log('currencySymbol', currencySymbol);
</script>

{#if $cartIsOpen}
    <!-- Backdrop -->
    <div 
        class="fixed inset-0 bg-black/50 z-40"
        on:click={() => cart.toggle()}
        transition:fly={{ duration: 200, opacity: 0 }}
    ></div>

    <!-- Side Cart -->
    <div 
        class="fixed top-0 right-0 h-full w-full max-w-md bg-base-100 shadow-xl z-50 flex flex-col"
        transition:fly={{ x: 400, duration: 300, easing: quintOut }}
    >
        <div class="flex justify-between items-center p-4 border-b">
            <h3 class="font-bold text-lg">Your Cart</h3>
            <button 
                class="btn btn-ghost btn-sm" 
                on:click={() => cart.toggle()}
            >
                <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                </svg>
            </button>
        </div>
        
        <div class="flex-1 overflow-y-auto p-4">
            {#if $cartItems.length === 0}
                <div class="h-full flex flex-col items-center justify-center text-center">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-16 w-16 text-base-300 mb-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z" />
                    </svg>
                    <p class="text-lg mb-2">Your cart is empty</p>
                    <p class="text-base-content/70 mb-4">Add some items to get started</p>
                    <button 
                        class="btn btn-primary" 
                        on:click={() => cart.toggle()}
                    >
                        Continue Shopping
                    </button>
                </div>
            {:else}
                <div class="space-y-4">
                    {#each $cartItems as item}
                        <div class="flex gap-4 pb-4 border-b">
                            {#if item.thumbnail}
                                <img 
                                    src={item.thumbnail} 
                                    alt={item.title}
                                    class="w-20 h-20 object-cover rounded-lg bg-base-200"
                                />
                            {/if}
                            <div class="flex-1">
                                <div class="flex justify-between">
                                    <div>
                                        <h3 class="font-medium">{item.title}</h3>
                                        <p class="text-sm text-base-content/70">
                                            {#if item.variant && item.variant.title}
                                                {item.variant.title}
                                            {/if}
                                        </p>
                                    </div>
                                    <button 
                                        class="btn btn-ghost btn-sm" 
                                        on:click={() => cart.remove(item.id)}
                                    >
                                        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                                        </svg>
                                    </button>
                                </div>
                                
                                <div class="flex justify-between items-end mt-2">
                                    <div class="join">
                                        <button 
                                            class="btn btn-sm join-item" 
                                            on:click={() => cart.update(item.id, item.quantity - 1)}
                                        >-</button>
                                        <input 
                                            type="text"
                                            class="input input-bordered input-sm w-14 join-item text-center"
                                            value={item.quantity}
                                            on:change={(e) => {
                                                const target = e.target as HTMLInputElement;
                                                const value = parseInt(target.value) || 1;
                                                if (value >= 1) {
                                                    cart.update(item.id, value);
                                                }
                                            }}
                                        />
                                        <button 
                                            class="btn btn-sm join-item"
                                            on:click={() => cart.update(item.id, item.quantity + 1)}
                                        >+</button>
                                    </div>
                                    <p class="font-medium">{calculateItemTotal(item)} {currencySymbol}</p>
                                </div>
                            </div>
                        </div>
                    {/each}
                </div>
            {/if}
        </div>

        {#if $cartItems.length > 0}
            <div class="border-t p-4 space-y-4">
                <div class="flex justify-between items-center">
                    <span class="text-lg font-bold">Total:</span>
                    <span class="text-lg font-bold">{calculateTotal($cartItems)} {currencySymbol}</span>
                </div>
                
                <div class="grid gap-2">
                    <a href="/checkout" class="btn btn-primary w-full">
                        Proceed to Checkout
                    </a>
                    <button 
                        class="btn btn-ghost btn-sm w-full" 
                        on:click={() => cart.toggle()}
                    >
                        Continue Shopping
                    </button>
                </div>
            </div>
        {/if}
    </div>
{/if}