<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\RequestDataLog;
use Illuminate\Support\Facades\File;
use Stevebauman\Location\Facades\Location;

class RequestLogController extends Controller
{
    public function index(Request $request)
    {
        // $query = RequestDataLog::query();
        $sevenDaysAgo = Carbon::now()->subDays(7);

        $query = RequestDataLog::where('created_at', '>=', $sevenDaysAgo);

        if ($request->search) {
            $query->where(function ($q) use ($request) {
                $q->where('domain', 'LIKE', "%{$request->search}%")
                ->orWhere('purchase_code', 'LIKE', "%{$request->search}%");
                //   ->orWhere('license_key', 'LIKE', "%{$request->search}%");
            });
        }

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

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

        if ($request->ip_address) {
            $query->where(function ($q) use ($request) {
                $q->where('end_user_ip', $request->ip_address)
                ->orWhere('host_server_ip', $request->ip_address);
            });
        }

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

        $sortBy = $request->get('sort_by', 'id');
        $sortDir = $request->get('sort_dir', 'desc');

        if (!in_array($sortBy, ['id', 'created_at'])) {
            $sortBy = 'id';
        }

        if (!in_array($sortDir, ['asc', 'desc'])) {
            $sortDir = 'desc';
        }

        $query->orderBy($sortBy, $sortDir);

        // Pagination
        $requestlogs = $query->paginate(10)->appends($request->all());

        // For dropdowns
        $domains = RequestDataLog::select('domain')->distinct()->pluck('domain');
        $products = RequestDataLog::select('product_slug')->distinct()->pluck('product_slug');
        $ips = RequestDataLog::select('end_user_ip')->distinct()->pluck('end_user_ip');
        $licenses = RequestDataLog::select('purchase_code')->distinct()->pluck('purchase_code');
        $csvDir = storage_path('app/request-logs');

        if (!File::exists($csvDir)) {
            File::makeDirectory($csvDir, 0755, true);
        }

        $csvFiles = collect(File::files($csvDir))
            ->map(fn ($file) => $file->getFilename());

        return view('admin.requestlogs.index', compact(
            'csvFiles',
            'requestlogs',
            'domains',
            'products',
            'ips',
            'licenses'
        ));
    }
    
    public function show($id)
    {
        $log = RequestDataLog::findOrFail($id);
        $location = Location::get($log->end_user_ip);
        // dd($location);

        return view('admin.requestlogs.show', compact('log', 'location'));
    }

    public function download($file)
    {
        $path = storage_path('app/request-logs/' . $file);

        if (!file_exists($path)) {
            abort(404);
        }

        return response()->download($path);
    }


}
