<?php

namespace App\Http\Controllers\Admin;

use App\Models\AdminProfile;
use App\Models\Admin;
use App\Models\Company;
use App\Models\Subscription;
use App\Models\Type;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller
{

    public function __construct()
    {
       $this->middleware('auth:admin-web');
    }

    public function index()
    {
        $auth_id = Auth::user()->id;
        $user = Admin::findOrFail($auth_id);

        if (AdminProfile::where('admin_id', '=', $auth_id)->count() > 0) {
            //profile found
        } else {
            // profile not found
            $user->assignRole($user->role_name);
            $profile = AdminProfile::create([
                'phone_number' => '',
                'city_name' => '',
                'age' => '',
                'gender' => '',
                'profile_pic' => 'app-assets/images/user.png',
                'admin_id' => $auth_id
            ]);
        }
        $companies = Company::with(['subscription'=>function($query){
              $query->with('type');
            }])->get();
        $trial = Type::where('type_name', 'تجربة')->first();
        $trial_subscriptions = Subscription::where('type_id', $trial->id)->where('status', 'active')->get();
        $about_end_subscriptions = Subscription::where('end_date','LIKE','%'.date('Y-m').'%')->where('status', 'active')->get();
        $active_subscriptions = Subscription::where('status', 'active')->get();
        $not_trial_subscriptions = Subscription::where('type_id','!=', $trial->id)->get();
        return view('admin.home', compact('user','active_subscriptions',
            'trial_subscriptions','about_end_subscriptions', 'companies','not_trial_subscriptions'));
    }
}
