<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\ContractSubItemsHistory;
use App\Http\Controllers\Api\ContractSubItemsController;

class ContractSubItems extends Model
{
    use HasFactory;
    public static function boot()
    {
        parent::boot();

        self::creating(function($model){
        });

        self::created(function($model){
            auth()->user()->addActivityComplete(1005,$model);
        });

        self::updating(function($model){
            auth()->user()->addActivityComplete(1006,$model);
        });

        self::updated(function($model){
        });

        self::deleting(function($model){
            auth()->user()->addActivityComplete(1007,$model);
        });

        self::deleted(function($model){
        });
    }


    protected $fillable = [
        'project_title',
        'project_type_id',
        'project_type_fa',
        'progress_project',
        'operation_type_id1',
        'operation_type_fa1',
        'operation_type_unit1',
        'operation_type_amount1',
        'operation_type_progress1',
        'operation_type_tonaj1',
        'operation_type_id2',
        'operation_type_fa2',
        'operation_type_unit2',
        'operation_type_amount2',
        'operation_type_progress2',
        'operation_type_tonaj2',
        'operation_type_id3',
        'operation_type_fa3',
        'operation_type_unit3',
        'operation_type_amount3',
        'operation_type_progress3',
        'operation_type_tonaj3',
        'operation_type_id4',
        'operation_type_fa4',
        'operation_type_unit4',
        'operation_type_amount4',
        'operation_type_progress4',
        'operation_type_tonaj4',
        'project_type_fa_for_chart',
        'city_id',
        'city_fa',
        'province_id',
        'province_fa',
        'axis_type_id',
        'axis_type_fa',
        'axis_name_id',
        'axis_name_fa',
        'start_lat',
        'end_lat',
        'start_lng',
        'end_lng',
        'followup_priority_project_id',
        'followup_priority_project',
        'priority_project',
        'priority_project_fa',
        'operator_name',
        'operator_phone',
        'created_at_fa',
        'last_status_id',
        'last_status_fa',
        'last_status_fa_for_chart',
        'last_digit_project',
        'last_function_operator',
        'last_payable_operator',
        'complete_payable',
        'updated_at_fa',
        'image_path1',
        'image_path2',
        'unique_code',
        'required_bitumen',
        'used_bitumen',
        'bridge_number',
        'contract_id',
        'contract_date_from_parent_contract',
        'start_way_id',
        'end_way_id',
        'user_id'
    ];

    protected $table = 'contract_subitems';


    public function Contracts()
    {
        return $this->belongsTo(Contracts::class, 'contract_id');
    }

    public function histories()
    {
        return $this->hasMany(ContractSubItemsHistory::class, 'subitem_id', 'id');
    }
    
    /// Setters
    public function setAxisTypeFa($id)
    {
        if ($id == '1') {
            $axis_type_fa = 'آزادراه';
        } elseif ($id == '2') {
            $axis_type_fa = 'بزرگراه';
        } elseif ($id == '3') {
            $axis_type_fa = 'اصلی';
        } elseif ($id == '4') {
            $axis_type_fa = 'فرعی';
        } elseif ($id == '5') {
            $axis_type_fa = 'روستایی';
        } elseif ($id == '0') {
            return;
        }

        $this->attributes['axis_type_fa'] = $axis_type_fa;
    }

    public function setPriorityProjectFa($id)
    {
        if ($id == '1') {
            $priority_project_fa = 'کم';
        } elseif ($id == '2') {
            $priority_project_fa = 'متوسط';
        } elseif ($id == '3') {
            $priority_project_fa = 'زیاد';
        } elseif ($id == '0') {
            return;
        }

        $this->attributes['priority_project_fa'] = $priority_project_fa;
    }
    
    public function setFollowupPriorityProjectFa($id)
    {
        if ($id == '1') {
            $followup_priority_project = 'کم(ماهیت محلی و منطقه ای بدون پیگیری های ویژه در سطح ملی)';
        } elseif ($id == '2') {
            $followup_priority_project = 'متوسط (ماهیت ملی یا استانی)';
        } elseif ($id == '3') {
            $followup_priority_project = 'زیاد (مورد تاکید و پیگیری ویژه مسیولین محلی یا ملی)';
        } else {
            return;
        }
        

        $this->attributes['followup_priority_project'] = $followup_priority_project;
    }

    public function setProjectTypeFa($id)
    {         
        $this->attributes['project_type_fa'] = (new ContractSubItemsController())->getAllOperationList()[$id]['farsi'];
        $this->attributes['project_type_fa_for_chart'] = (new ContractSubItemsController())->getAllOperationList()[$id]['chart'];
    }

    public function setLastStatusFa($id)
    {
        if ($id == '1') {
            $last_status['fa'] = 'در حال تجهیز کارگاه';
            $last_status['chart'] = 'در حال تجهیز کارگاه';
        } elseif ($id == '2') {
            $last_status['fa'] = 'در حال اجرا';
            $last_status['chart'] = 'در حال اجرا';
        } elseif ($id == '3') {
            $last_status['fa'] = 'خاتمه یافته و بهره برداری شده';
            $last_status['chart'] = 'خاتمه یافته';
        } elseif ($id == '4') {
            $last_status['fa'] = 'در دست مناقصه';
            $last_status['chart'] = 'در دست مناقصه';
        } elseif ($id == '0') {
            return;
        }

        $this->attributes['last_status_fa'] = $last_status['fa'];
        $this->attributes['last_status_fa_for_chart'] = $last_status['chart'];
    }

    public static function getProjectQuery($id)
    {
        if ($id == '1') {
            $projectQuery = \App\Models\RoadMaintenanceProject::query();
        } elseif ($id == '2') {
            $projectQuery = \App\Models\RoadDangerPrevention::query();
        } elseif ($id == '3') {
            $projectQuery = \App\Models\RoadUpgradeSafety::query();
        } elseif ($id == '4') {
            $projectQuery = \App\Models\RoadTechnicalBuilding::query();
        } elseif ($id == '5') {
            $projectQuery = \App\Models\RoadTollhouseManagement::query();
        } elseif ($id == '0') {
            return;
        }

        return $projectQuery;
    }

    public static function checkProgressProjectCondition($status_id, $progress)
    {
        if ($status_id == '1') {
            $progress_project = '10';
        } elseif ($status_id == '2') {
            if ($progress > 10 && $progress <= 99) {
                $progress_project = $progress;
            } else {
                return response()->json(['message' => 'progress project is not valid'], 400);

            }
        } elseif ($status_id == '3') {
            $progress_project = '100';
        } elseif ($status_id == '4') {
            $progress_project = '5';
        } elseif ($status_id == '0') {
            return;
        }

        return $progress_project;
    }

}