<?php

namespace App\Http\Controllers;

use App\Models\DeviceLog;
use App\Models\License;
use Illuminate\Http\Request;

class DeviceLogController extends Controller
{
    public function index(Request $request)
    {
        $query = DeviceLog::query()->with('license');

        if ($request->license_id) {
            $query->where('license_id', $request->license_id);
        }

        if ($request->device_type) {
            $query->where('device_type', $request->device_type);
        }

        if ($request->action) {
            $query->where('action', $request->action);
        }

        if ($request->status === 'active') {
            $query->where('is_active', true);
        } elseif ($request->status === 'inactive') {
            $query->where('is_active', false);
        }

        $deviceLogs = $query->orderBy('last_seen', 'desc')->paginate(20);

        return view('devices.index', compact('deviceLogs'));
    }

    public function show(DeviceLog $deviceLog)
    {
        $deviceLog->load('license');
        
        $recentActivity = DeviceLog::where('device_id', $deviceLog->device_id)
            ->orderBy('created_at', 'desc')
            ->limit(20)
            ->get();

        return view('devices.show', compact('deviceLog', 'recentActivity'));
    }

    public function block(DeviceLog $deviceLog)
    {
        $deviceLog->update([
            'is_active' => false,
            'action' => 'blocked',
        ]);

        $license = $deviceLog->license;
        if ($license) {
            $license->decrementDeviceCount();
        }

        return redirect()->back()
            ->with('success', 'Device blocked successfully!');
    }

    public function unblock(DeviceLog $deviceLog)
    {
        $deviceLog->update([
            'is_active' => true,
            'action' => 'login',
        ]);

        $license = $deviceLog->license;
        if ($license && $license->canAddDevice()) {
            $license->incrementDeviceCount();
        }

        return redirect()->back()
            ->with('success', 'Device unblocked successfully!');
    }

    public function destroy(DeviceLog $deviceLog)
    {
        $license = $deviceLog->license;
        
        $deviceLog->delete();

        if ($license && $deviceLog->is_active) {
            $license->decrementDeviceCount();
        }

        return redirect()->route('devices.index')
            ->with('success', 'Device log deleted successfully!');
    }

    public function cleanup()
    {
        $staleDevices = DeviceLog::where('is_active', true)
            ->where('last_seen', '<', now()->subMinutes(30))
            ->get();

        foreach ($staleDevices as $device) {
            $device->update(['is_active' => false]);
            $license = $device->license;
            if ($license) {
                $license->decrementDeviceCount();
            }
        }

        return redirect()->route('devices.index')
            ->with('success', 'Cleaned up ' . $staleDevices->count() . ' stale devices!');
    }
}