<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

class Job extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'company_id',
        'title',
        'location',
        'type',
        'email',
        'currency',
        'min_salary',
        'max_salary',
        'description',
        'roles',
        'how_to_apply',
        'status',
        'slug'
    ];


    protected $casts = [
        'status' => 'integer', // Cast as boolean
    ];

    protected static function boot(){
        parent::boot();

        static::deleting(function ($job) {
            $job->jobRequests()->delete();
        });
    }

    public function company(){
        return $this->belongsTo(Company::class);
    }

    public function savedItems(){
        return $this->hasMany(SavedItem::class, 'item_id')->where('type', 'JOB');
    }

    public function jobRequests(){
        return $this->hasMany(JobRequest::class, 'job_id', 'id');
    }

    public function clicks(){
        return $this->hasMany(ItemClick::class, 'item_id')->where('type', 'Job');
    }

    public function views(){
        return $this->hasMany(ItemView::class, 'item_id')->where('type', 'Job');
    }

    public static function createJob($companyId, $title, $description, $location, $type, $email, $slug, $minSalary, $maxSalary, $currency='AED', $roles=null, $howToApply=null){
        return Job::create([
            'company_id' => $companyId,
            'title' => $title,
            'location' => $location,
            'type' => $type,
            'email' => $email,
            'currency' => $currency,
            'min_salary' => $minSalary,
            'max_salary' => $maxSalary,
            'description' => $description,
            'roles' => $roles,
            'how_to_apply' => $howToApply,
            'slug' => $slug
        ]);
    }

    public static function getJobs($jobTypes=[], $countries=[], $companyTypes=[], $searchTerms=null, $companyId=null, $getSaved=false, $status=1){
        $jobs = Job::with('company', 'savedItems')
        ->withCount(['savedItems as is_saved' => function ($query) {
            $query->where('user_id', Auth::id());
        }]);

        if($getSaved){
            $jobs = $jobs->whereHas('savedItems', function($q){
                return $q->where('user_id', Auth::id())->where('type', 'JOB');
            });
        }

        if(Auth::user() != null && $companyId != null && $companyId == Auth::user()->company->id){
            $jobs = $jobs->whereIn('status', [1, 0]);
        }else{
            $jobs = $jobs->where('status', $status);
        }
        
        if($companyId != null){
            $jobs = $jobs->where('company_id', $companyId);
        }
        if($searchTerms != null && $searchTerms != ""){
            $jobs = $jobs->where('title', 'LIKE', '%'.$searchTerms.'%');
        }

        if(!empty($jobTypes)){
            $jobs = $jobs->whereIn('type', $jobTypes);
        }

        if(!empty($countries)){
            $jobs = $jobs->whereIn('location', $countries);
        }

        if(!empty($companyTypes)){
            $jobs = $jobs->whereHas('company', function($q) use($companyTypes){
                $q->whereIn('type', $companyTypes);
            });
        }

        $jobs = $jobs->orderBy('id', 'desc')->get();

        return $jobs;
    }
}
