<?php

namespace App\Console\Commands;

use App\Models\Admin;
use App\Models\Permission;
use App\Models\Reward;
use App\Models\Role;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\File;

class TestCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tc';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        // Role::pluck('name');
        // Permission::pluck('name');

        $permission = Permission::create([
            'name' => 'manage_user:dummy_users_edit',
            'guard_name' => 'admin'
        ]);
        $superadmin = Role::findByName('super admin', 'admin');
        // $superadmin->givePermissionTo($permission);

        // $permissions = Permission::all();
        // $superadmin->givePermissionTo($permissions);

        // $reward = Reward::latest()->first();
        // dd($reward->win_status);
        // app(Sms::class)->mobileSmsVerification(User::first());

        // $permissions = Permission::all();
        // File::put('permissions.json', $permissions->toJson(JSON_PRETTY_PRINT));

        // $permission = Permission::create(['name' => 'file_list', 'guard_name' => 'admin']);
        // $superadmin = Role::findByName('super admin', 'admin');
        // $superadmin->givePermissionTo($permission);

        // $user = Admin::firstWhere("username", 'Rahul');
        // $permissions = $user->getAllPermissions();
        // File::put('temp.json', $permissions->toJson(JSON_PRETTY_PRINT));

        // $url = parse_url('?search=&date=12%2F04%2F2024');

        // parse_str($url['query'], $query);
        // $startDate = Carbon::parse(trim($query['date']))->setTimezone(config('app.local_timezone'))->format('Y-m-d');
        // dd($startDate);
        // $date = Carbon::now();
        // dd($date->setTimezone(config('app.local_timezone'))->format('d-m-y h:i:s'));

        // $now = Carbon::create(2024, 12, 24, 11, 0, 0); // Example date
        // Carbon::setTestNow($now);

        // $xUnit = '3 hours';
        // $expiredAt = $this->setExpiredAt($xUnit);

        // echo "Current time: " . $now->toDateTimeString() . "\n";
        // echo "Expired at: " . $expiredAt->toDateTimeString() . "\n";
    }

    function setExpiredAt($xUnit = '2 hours')
    {
        $now = Carbon::now(); // Current timestamp
        $startOfDay = $now->copy()->setTime(10, 0); // 10 AM of the same day
        $endOfDay = $now->copy()->setTime(18, 0); // 6 PM of the same day

        if ($now->between($startOfDay, $endOfDay)) {
            // If current time is within professional hours, add x units
            $expiredAt = $now->copy()->add($xUnit);
        } else if($now->isAfter($endOfDay)) {
            // Calculate the next professional start time
            $nextProfessionalTime = $now->copy()->addDay()->setTime(10, 0);

            // Add x units to the next professional time
            $expiredAt = $nextProfessionalTime->add($xUnit);
        } else {
            $expiredAt = $startOfDay->copy()->add($xUnit);
        }

        return $expiredAt;
    }
}
