<template>
  <div class="desktop-container">
    <!-- Top bar with user info -->
    <div class="navbar bg-base-300 px-4">
      <div class="navbar-start">
        <div class="text-xl font-bold">Profile</div>
      </div>
      <div class="navbar-end">
        <div class="dropdown dropdown-end">
          <div tabindex="0" role="button" class="btn btn-ghost btn-circle avatar">
            <div class="w-10 rounded-full bg-primary flex items-center justify-center">
              <span class="text-primary-content font-bold text-sm">
                {{ appStore.userInitials }}
              </span>
            </div>
          </div>
          <ul tabindex="0" class="menu menu-sm dropdown-content mt-3 z-[1] p-2 shadow bg-base-100 rounded-box w-52">
            <li><a @click="navigateToProfile">Profile</a></li>
            <li><a>Settings</a></li>
            <li><a @click="handleLogout">Logout</a></li>
          </ul>
        </div>
      </div>
    </div>

    <!-- Main content area -->
    <div class="flex-1 flex overflow-hidden">
      <!-- Sidebar -->
      <div class="w-64 bg-base-200 p-4">
        <ul class="menu">
          <li><a @click="navigateToDashboard">Dashboard</a></li>
          <li><a class="active">Profile</a></li>
          <li><a>Trading</a></li>
          <li><a>Portfolio</a></li>
          <li><a>Markets</a></li>
          <li><a @click="navigateToHuntingGround">Hunting Ground</a></li>
          <li><a>Analytics</a></li>
        </ul>
      </div>

      <!-- Main content -->
      <div class="flex-1 p-6 overflow-y-auto">
        <!-- User Info Section -->
        <div class="card bg-base-100 shadow-xl mb-6">
          <div class="card-body">
            <div class="flex items-center gap-6">
              <div class="w-20 h-20 rounded-full bg-primary flex items-center justify-center">
                <span class="text-primary-content font-bold text-2xl">
                  {{ appStore.userInitials }}
                </span>
              </div>
              <div>
                <h2 class="card-title text-2xl">{{ appStore.currentUser?.name || 'John Trader' }}</h2>
                <p class="text-base-content/70">{{ appStore.currentUser?.email || 'john@example.com' }}</p>
                <div class="badge badge-success mt-2">Pro Trader</div>
              </div>
            </div>
          </div>
        </div>

        <!-- Account Overview -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
          <div class="card bg-base-100 shadow-lg">
            <div class="card-body">
              <h3 class="card-title text-lg">Total Portfolio Value</h3>
              <div class="stat-value text-primary text-3xl">$125,340</div>
              <div class="text-success text-sm">↗ +8.2% today</div>
            </div>
          </div>

          <div class="card bg-base-100 shadow-lg">
            <div class="card-body">
              <h3 class="card-title text-lg">Available Balance</h3>
              <div class="stat-value text-secondary text-3xl">$25,680</div>
              <div class="text-base-content/70 text-sm">Ready to trade</div>
            </div>
          </div>

          <div class="card bg-base-100 shadow-lg">
            <div class="card-body">
              <h3 class="card-title text-lg">Total Profit/Loss</h3>
              <div class="stat-value text-accent text-3xl">+$12,450</div>
              <div class="text-success text-sm">↗ +15.6% all time</div>
            </div>
          </div>
        </div>

        <!-- Current Positions -->
        <div class="card bg-base-100 shadow-xl mb-6">
          <div class="card-body">
            <h3 class="card-title">Current Positions</h3>
            <div class="overflow-x-auto">
              <table class="table">
                <thead>
                  <tr>
                    <th>Token</th>
                    <th>Amount</th>
                    <th>Entry Price</th>
                    <th>Current Price</th>
                    <th>P&L</th>
                    <th>Change</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>
                      <div class="flex items-center gap-3">
                        <div class="w-8 h-8 rounded-full bg-orange-500 flex items-center justify-center text-white font-bold text-xs">
                          BTC
                        </div>
                        <span class="font-semibold">Bitcoin</span>
                      </div>
                    </td>
                    <td>0.25 BTC</td>
                    <td>$42,000</td>
                    <td>$45,200</td>
                    <td class="text-success font-semibold">+$800</td>
                    <td><span class="badge badge-success">+7.6%</span></td>
                  </tr>
                  <tr>
                    <td>
                      <div class="flex items-center gap-3">
                        <div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold text-xs">
                          ETH
                        </div>
                        <span class="font-semibold">Ethereum</span>
                      </div>
                    </td>
                    <td>5.5 ETH</td>
                    <td>$2,800</td>
                    <td>$2,650</td>
                    <td class="text-error font-semibold">-$825</td>
                    <td><span class="badge badge-error">-5.4%</span></td>
                  </tr>
                  <tr>
                    <td>
                      <div class="flex items-center gap-3">
                        <div class="w-8 h-8 rounded-full bg-purple-500 flex items-center justify-center text-white font-bold text-xs">
                          SOL
                        </div>
                        <span class="font-semibold">Solana</span>
                      </div>
                    </td>
                    <td>120 SOL</td>
                    <td>$98</td>
                    <td>$105</td>
                    <td class="text-success font-semibold">+$840</td>
                    <td><span class="badge badge-success">+7.1%</span></td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>

        <!-- Recent Transactions -->
        <div class="card bg-base-100 shadow-xl">
          <div class="card-body">
            <h3 class="card-title">Recent Transactions</h3>
            <div class="space-y-4">
              <div class="flex items-center justify-between p-4 border border-base-200 rounded-lg">
                <div class="flex items-center gap-3">
                  <div class="w-10 h-10 rounded-full bg-success flex items-center justify-center">
                    <svg class="w-5 h-5 text-success-content" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                    </svg>
                  </div>
                  <div>
                    <div class="font-semibold">Bought BTC</div>
                    <div class="text-sm text-base-content/70">Today, 2:30 PM</div>
                  </div>
                </div>
                <div class="text-right">
                  <div class="font-semibold">0.1 BTC</div>
                  <div class="text-sm text-base-content/70">$4,520</div>
                </div>
              </div>

              <div class="flex items-center justify-between p-4 border border-base-200 rounded-lg">
                <div class="flex items-center gap-3">
                  <div class="w-10 h-10 rounded-full bg-error flex items-center justify-center">
                    <svg class="w-5 h-5 text-error-content" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 12H4" />
                    </svg>
                  </div>
                  <div>
                    <div class="font-semibold">Sold ETH</div>
                    <div class="text-sm text-base-content/70">Yesterday, 11:45 AM</div>
                  </div>
                </div>
                <div class="text-right">
                  <div class="font-semibold">2.5 ETH</div>
                  <div class="text-sm text-base-content/70">$6,625</div>
                </div>
              </div>

              <div class="flex items-center justify-between p-4 border border-base-200 rounded-lg">
                <div class="flex items-center gap-3">
                  <div class="w-10 h-10 rounded-full bg-success flex items-center justify-center">
                    <svg class="w-5 h-5 text-success-content" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                    </svg>
                  </div>
                  <div>
                    <div class="font-semibold">Bought SOL</div>
                    <div class="text-sm text-base-content/70">2 days ago, 4:15 PM</div>
                  </div>
                </div>
                <div class="text-right">
                  <div class="font-semibold">50 SOL</div>
                  <div class="text-sm text-base-content/70">$4,900</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useAppStore } from '../stores/app';

const appStore = useAppStore();
const router = useRouter();

// Redirect if not authenticated
onMounted(() => {
  if (!appStore.isAuthenticated) {
    router.push('/login');
  }
});

const handleLogout = async () => {
  await appStore.logout();
  router.push('/login');
};

const navigateToDashboard = () => {
  router.push('/dashboard');
};

const navigateToProfile = () => {
  router.push('/profile');
};

const navigateToHuntingGround = () => {
  router.push('/hunting-ground');
};
</script>

<style scoped>
/* Page-specific styles if needed */
</style>
