#!/bin/bash

# Create Test Agency in Database

echo "=========================================="
echo "Creating Test Agency"
echo "=========================================="
echo ""

# Generate a UUID
AGENCY_ID=$(uuidgen | tr '[:upper:]' '[:lower:]')

echo "Creating agency with ID: $AGENCY_ID"
echo ""

# Create agency in database
docker-compose exec -T postgres psql -U flight_user -d flight_platform << EOF
INSERT INTO agencies (
    id,
    name,
    matricule_number,
    email,
    phone,
    address,
    city,
    country,
    postal_code,
    status,
    subscription_type,
    markup_percentage,
    credit_limit,
    created_at,
    updated_at
) VALUES (
    '$AGENCY_ID',
    'Test Travel Agency',
    'TEST001',
    'test@agency.com',
    '+34600000000',
    'Calle Test 123',
    'Madrid',
    'Spain',
    '28001',
    'ACTIVE',
    'PREMIUM',
    5.0,
    10000.00,
    NOW(),
    NOW()
);

-- Create wallet for the agency
INSERT INTO wallets (
    id,
    agency_id,
    balance,
    currency,
    created_at,
    updated_at
) VALUES (
    gen_random_uuid(),
    '$AGENCY_ID',
    5000.00,
    'EUR',
    NOW(),
    NOW()
);

-- Verify creation
SELECT 
    id,
    name,
    matricule_number,
    email,
    status,
    subscription_type,
    markup_percentage
FROM agencies 
WHERE id = '$AGENCY_ID';
EOF

echo ""
echo "=========================================="
echo "✅ Test Agency Created Successfully!"
echo "=========================================="
echo ""
echo "Agency Details:"
echo "  ID: $AGENCY_ID"
echo "  Name: Test Travel Agency"
echo "  Matricule: TEST001"
echo "  Email: test@agency.com"
echo "  Status: ACTIVE"
echo "  Subscription: PREMIUM"
echo "  Markup: 5.0%"
echo "  Wallet Balance: 5000.00 EUR"
echo ""
echo "=========================================="
echo "Use this Agency ID in Swagger UI:"
echo "=========================================="
echo ""
echo "$AGENCY_ID"
echo ""
echo "Copy this ID and use it as the 'agencyId' query parameter"
echo "when testing the flight search endpoint."
echo ""

# Save to file for easy access
echo "$AGENCY_ID" > /tmp/test_agency_id.txt
echo "Agency ID saved to: /tmp/test_agency_id.txt"
echo ""
