<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\Company;
use App\Models\Package;
use App\Models\Subscription;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PackagesController extends Controller
{
    # not comply with psr-4 autoloading standard. Skipping. 
    
    public function index(){
        $company_id = Auth::user()->company_id;
        $packages =  Package::where('price','>',0)->get();
        $subscription = Subscription::where('company_id',$company_id)->first();

        return view('client.packages.index',compact('packages','subscription'));
    }

    public function trail(){
            $company = Company::findOrFail(Auth::user()->company_id);
            $package = Package::findOrFail(1);
            
            $subscription = Subscription::create([
            'package_id' => $package->id,
            'period' => $package->types[0]->period,
            'type_id' => $package->types[0]->id,
            'start_date' => date('Y-m-d'),
            'end_date' => date("Y-m-d", strtotime("+".$package->types[0]->period." day")),
            'status' => 'active',
            'company_id' => $company->id
        ]);

        return redirect(route('client.home'));
    }

    public function checkout($id){

        $package = Package::where('id','!=',1)->findOrFail($id);

        return view('client.packages.checkout',compact('package'));


    }
}
