<?php

namespace App\Console\Commands;

use App\Facilities;
use App\Packages;
use App\Subscription;
use App\SubscriptionFacilities;
use App\Transactions;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Laravel\Cashier\Cashier;
use Stripe\Charge;
use Stripe\Customer;

class SubscriptionCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'subscriptiont:cron';

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

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixeds
     */
    public function handle()
    {
        \Log::info("Cron is working fine!");
        $tomorrow = Carbon::now()->addDay();
        $threeDaysFromNow = Carbon::now()->addDays(3);

        $subscriptions = Subscription::with('transaction')->whereBetween('ends_at', [$tomorrow, $threeDaysFromNow])->get();

        foreach ($subscriptions as $subscription) {

            $customer = Customer::retrieve($subscription->stripe_id);

            // Charge the customer with the specified amount and currency
            $charge = Charge::create([
                'amount' => $subscription->transaction->total * 100,
                // Replace with the actual amount in cents
                'currency' => 'usd',
                // Replace with the actual currency
                'customer' => $customer->id,
            ]);
            if ($charge == 'succeeded') {
                $tranactions = [
                    "customer_id" => $subscription->stripe_id,
                    "subscription_id" => $subscription->id,
                    "paymentMethod" => 'Stripe',
                    "boilerAge" => $subscription->transaction->boilerAge,
                    "package_id" => $subscription->transaction->package_id,
                    "type" => $subscription->transaction->type,
                    "excess_id" => $subscription->transaction->excess_id,
                    "price" => $$subscription->transaction->price,
                    "vat" => $subscription->transaction->vat,
                    "total" => $subscription->transaction->total_price,
                    "startDate" => $subscription->transaction->anchorDate,
                    "created_at" => Carbon::now()
                ];

                $tranactions_id = Transactions::insertGetId($tranactions);


                $subscriptions = [
                    "status" => 1,
                    "ends_at" => date('Y-m-d', strtotime($subscription->start_date . ' + 1 months')),
                ];
                $subscription_id = Subscription::insertGetId($subscriptions);

                $selectedFacilities = Facilities::where('subcription_id', $subscription->id)->get();
                $package = Packages::find($subscription->transaction->package_id);

                $package_id = $package->id;

                $facilities = Facilities::join('package_facilities', 'package_facilities.facility_id', '=', 'facilities.id')
                    ->where("package_facilities.package_id", $package_id)
                    ->where("status", 1);



                if (!empty($selectedFacilities)) {
                    $facilities = $facilities->whereIn("facilities.id", $selectedFacilities);
                }

                $facilities = $facilities->get();
                //echo count($facilities);


                $j = 0;
                $inputs = array();
                foreach ($facilities as $facility) {
                    $inputs[$j]["transaction_id"] = $tranactions_id;
                    $inputs[$j]["subcription_id"] = $subscription->id;
                    $inputs[$j]["facility_id"] = $facility->facility_id;
                    $inputs[$j]["title"] = $facility->title;
                    $inputs[$j]["title"] = $facility->title;
                    $inputs[$j]["price"] = $facility->hasPrice == 1 ? $facility->price : 0;
                    $inputs[$j]["created_at"] = date("Y-m-d H:i:s");
                    $j++;
                }

                SubscriptionFacilities::insert($inputs);






            }

        }





        $this->info('Demo:Cron Cummand Run successfully!');
    }
}