#!/bin/bash
set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

echo -e "${GREEN}🚀 Dew Finance Vault Deployment Script${NC}"
echo "==========================================="

# Check if dev-account exists
DEV_ACCOUNT_FILE="$SCRIPT_DIR/neardev/dev-account"

if [ ! -f "$DEV_ACCOUNT_FILE" ]; then
    echo -e "${YELLOW}📝 No dev account found. Creating new account...${NC}"
    sh "$SCRIPT_DIR/utils/generate_account.sh"
else
    echo -e "${GREEN}✅ Using existing dev account:${NC} $(cat "$DEV_ACCOUNT_FILE")"
fi

export CONTRACT_ID="$(cat "$DEV_ACCOUNT_FILE")"

echo -e "${YELLOW}🔨 Building and deploying contract...${NC}"
sh "$SCRIPT_DIR/2_deploy.sh"

echo ""
echo -e "${YELLOW}🔧 Initializing contract...${NC}"
sh "$SCRIPT_DIR/3_init.sh"

echo ""
echo -e "${GREEN}🎉 Deployment completed successfully!${NC}"
echo "==========================================="
echo "Contract Address: $CONTRACT_ID"
echo ""
echo "Next steps:"
echo "1. Register for storage: near call $CONTRACT_ID storage_deposit '{}' --accountId YOUR_ACCOUNT --deposit 0.1"
echo "2. Check contract state: near view $CONTRACT_ID asset"
echo ""
echo "📋 Useful commands:"
echo "  # View asset"
echo "  near view $CONTRACT_ID asset"
echo ""
echo "  # View token metadata"
echo "  near view $CONTRACT_ID ft_metadata"
echo ""
echo "  # Register for storage"
echo "  near call $CONTRACT_ID storage_deposit '{}' --accountId YOUR_ACCOUNT.testnet --deposit 0.1"