<?php

namespace App\Console\Commands;

use App\Models\Admin;
use App\Models\Gateway;
use App\Models\Permission;
use App\Models\Reward;
use App\Models\Role;
use App\Models\SupportTicket;
use App\Models\Transaction;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\File;
use App\Mail\TestMail;
use App\Notifications\VerificationNotification;
use Illuminate\Support\Facades\Mail;


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";

        // $ticket = SupportTicket::latest()->first();
        // dd($ticket->toArray());

        // $paymentMethod = Gateway::firstWhere('alias', 'Razorpay');
        // $params = json_decode($paymentMethod->gateway_parameters, true);
        // $paymentMethod->gateway_parameters = json_encode([...$params, 'webhook_secret' => ['title' => 'Webhook Secret',  'value' => 'eofap', 'global' => true]]);
        // $paymentMethod->save();
        // dd($params);
        // dd($paymentMethod->refresh()->toArray());

        // $transaction = Transaction::whereJsonContains('metadata->id', 'order_Pfoju2dRI8RRTh')->first();
        // dd($transaction->toArray());

        // $permissions = ['manage_user:mobile_verification', 'manage_user:email_verification', 'manage_user:kyc_verification'];
        // $perms = [];
        // foreach ($permissions as $perm) {
        //     $perms[]  = Permission::create([
        //         'name' => $perm,
        //         'guard_name' => 'admin'
        //     ]);
        // }

        // $role = Role::firstWhere('name', 'super admin');
        // $role->givePermissionTo($perms);

        $user = User::firstWhere('username', 'admin');
        $user->forceFill(['ver_code' => 232344]);
        $user->save();
        $user->notify(new VerificationNotification());
    }

    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;
    }
}
