﻿<?php

namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Model;
use App\Scopes\OrganisationScope;


class Plan extends Model
{
    protected $table = 'accounting_plans'; 
    protected $fillable = [
        'name',
        'price',
        'duration',
        'max_users',
        'max_customers',
        'max_venders',
        'storage_limit',
        'description',
        'image',
        'enable_chatgpt',
        'trial',
        'trial_days',
        'is_disable',
        'organisation_id',


    ];


    protected static function booted()
    {
        static::addGlobalScope(new OrganisationScope);
    
      // Automatically set the organisation_id before creating the model
            static::creating(function ($model) {
                if (auth()->check()) {
                    $model->organisation_id = auth()->user()->organisation_id;
                }
            });
    }
    public static $arrDuration = [
        'lifetime' => 'Lifetime',
        'month' => 'Per Month',
        'year' => 'Per Year',
    ];

    public function status()
    {
        return [
            __('Lifetime'),
            __('Per Month'),
            __('Per Year'),
        ];
    }

    public static function total_plan()
    {
        return Plan::count();
    }

    public static function most_purchese_plan()
    {
        $free_plan = Plan::where('price', '<=', 0)->first()->id;

        return User:: select(\DB::raw('count(*) as total'))->where('type', '=', 'company')->where('plan', '!=', $free_plan)->groupBy('plan')->first();
    }