<?php

namespace App\Models\Administration;

use App\Models\Application\Client;
use App\Models\Application\Group;
use App\Models\Application\Loan;
use App\Models\Application\LoanType;
use App\Models\Application\Zone;
use App\Models\Bussiness\Collaborator;
use App\Models\Bussiness\Position;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Office extends Model
{
    use HasFactory;

    protected $table = 'sy_offices';
    public $timestamps = false;

    protected $fillable = [
        'id',
        'fk_site',
        'officename',
        'visible',
        'available',
        'fk_usercreator',
        'created_at',
        'fk_userupdater',
        'udapted_at',
        'is_active',
    ];

    protected $casts = [
        'id' => 'integer',
        'fk_site' => 'integer',
        'visible' => 'boolean',
        'available' => 'boolean',
        'fk_usercreator' => 'integer',
        'created_at' => 'timestamp',
        'fk_userupdater' => 'integer',
        'updated_at' => 'timestamp',
        'is_active' => 'boolean'
    ];

    protected $hidden = [
        'fk_site',
        'fk_usercreator',
        'fk_userupdater',
        'is_active',
    ];

    protected static function booted()
    {
        static::updated(function ($office) {
            Log::channel('office')->info('Office Updated', [
                'id' => $office->id,
                'fk_userupdater' => $office->fk_userupdater,
                'updated_at' => Carbon::now(),
                'changes' => $office->getChanges()
            ]);
        });
    }

    public function site()
    {
        return $this->belongsTo(Site::class, 'fk_site', 'id');
    }

    public function users()
    {
        return $this->hasManyThrough(User::class, Site::class, 'fk_site', 'fk_site');
    }

    public function positions()
    {
        return $this->hasMany(Position::class, 'fk_office', 'id')->select('id', 'positionname', 'fk_office');
    }

    public function collaborators()
    {
        return $this->hasMany(Collaborator::class, 'fk_office', 'id')->select(
            'fk_office',
            'ct_collaborators.id',
            DB::raw("CONCAT(firstname, ' ', lastname, ' ', secondlastname) as fullname"),
            'fk_position'
        )->where([
            ['is_active', true]
        ])->with('position');
    }

    public function loan_types()
    {
        return $this->hasMany(LoanType::class, 'fk_office', 'id')->select('id', 'fk_office', 'loantypename', 'period', 'duration', 'weeklypayment', 'interesrate', 'alloweddays');
    }

    public function clients()
    {
        return $this->hasMany(Client::class, 'fk_office', 'id')->select(
            'id',
            'fk_office',
            DB::raw("CONCAT(firstname, ' ', lastname, ' ', secondlastname) as fullname"),
            'curp',
            'colony',
            'address',
            'phonenumber',
            'fk_client_status',
            'fk_endorsement_status'
        );
    }

    public function zones()
    {
        return $this->hasMany(Zone::class, 'fk_office', 'id')->select('id', 'fk_site', 'fk_office', 'fk_city', 'zonename', 'fk_executive');
    }

    public function groups()
    {
        return $this->hasMany(Group::class, 'fk_office', 'id')->select('id', 'fk_site', 'fk_office', 'fk_zone', 'groupname', 'fk_promoter');
    }

    public function loans()
    {
        return $this->hasMany(Loan::class, 'fk_office', 'id');
    }

    public function office_user()
    {
        return $this->hasMany(OfficeUser::class, 'fk_office', 'id');
    }

    public function user_creator()
    {
        return $this->hasOne(User::class, 'id', 'fk_usercreator')->select('id', 'username');
    }

    public function user_updater()
    {
        return $this->hasOne(User::class, 'id', 'fk_userupdater')->select('id', 'username');
    }
}
