<?php

namespace App\Http\Controllers;

use App\Models\BotSetting;
use App\Models\Notification;
use App\Services\DashboardService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class DashboardController extends Controller
{
    public function __construct(private DashboardService $dashboard) {}

    public function overview(): View
    {
        return view('trading.overview', [
            'data' => $this->dashboard->overview(auth()->user()),
            'pageTitle' => 'Trading Desk',
            'pageSubtitle' => 'FlatTrade account — Real orders & balance',
        ]);
    }

    public function holdings(): View
    {
        return view('trading.holdings', [
            'data' => $this->dashboard->overview(auth()->user()),
            'pageTitle' => 'Holdings',
            'pageSubtitle' => 'Open positions and holdings',
        ]);
    }

    public function pnl(): View
    {
        return view('trading.pnl', [
            'data' => $this->dashboard->overview(auth()->user()),
            'pageTitle' => 'P&L Report',
            'pageSubtitle' => 'Realized and unrealized profit & loss',
        ]);
    }

    public function orders(): View
    {
        return view('trading.orders', [
            'data' => $this->dashboard->overview(auth()->user()),
            'pageTitle' => 'Orders',
            'pageSubtitle' => 'Order book and history',
        ]);
    }

    public function watchlist(): View
    {
        return view('trading.watchlist', [
            'data' => $this->dashboard->overview(auth()->user()),
            'pageTitle' => 'Watchlist',
            'pageSubtitle' => 'ATM±5 strikes monitored by the bot',
        ]);
    }

    public function broker(): View
    {
        return redirect()->route('broker');
    }

    public function agent(): View
    {
        return view('trading.agent', [
            'data' => $this->dashboard->overview(auth()->user()),
            'pageTitle' => 'Agent',
            'pageSubtitle' => 'Bot activity and agent log',
        ]);
    }

    public function toggleBot(Request $request): RedirectResponse
    {
        $settings = BotSetting::firstOrCreate(['user_id' => auth()->id()]);
        $settings->update(['bot_enabled' => ! $settings->bot_enabled]);

        return back()->with('status', $settings->bot_enabled ? 'Bot enabled.' : 'Bot disabled.');
    }

    public function startBot(): RedirectResponse
    {
        $settings = BotSetting::firstOrCreate(['user_id' => auth()->id()]);
        $settings->update([
            'bot_enabled' => true,
            'auto_trade' => true,
            'bot_status' => 'running',
            'started_at' => now(),
        ]);

        Notification::create([
            'user_id' => auth()->id(),
            'type' => 'bot_started',
            'title' => 'Bot Started',
            'message' => 'Auto trade engine started. Bot will scan every 3 minutes during market hours.',
        ]);

        return back()->with('status', 'Bot started.');
    }

    public function stopBot(): RedirectResponse
    {
        $settings = BotSetting::firstOrCreate(['user_id' => auth()->id()]);
        $settings->update([
            'auto_trade' => false,
            'bot_status' => 'stopped',
        ]);

        Notification::create([
            'user_id' => auth()->id(),
            'type' => 'bot_stopped',
            'title' => 'Bot Stopped',
            'message' => 'Auto trade engine stopped.',
        ]);

        return back()->with('status', 'Bot stopped.');
    }
}
