<?php

namespace App\Models\Application;

use App\Models\Administration\Office;
use App\Models\Administration\Site;
use App\Models\Administration\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class GroupReport extends Model
{
    use HasFactory;

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

    protected $fillable = [
        'id',
        'fk_site',
        'fk_office',
        'fk_group',
        'reportdate',
        'amountdue',
        'amountfailed',
        'subtotalamount',
        'amountrecovered',
        'totalamount',
        'incomingamount',
        'outgoingamount',
        'extraweekamount',
        'totaldeliveryamount',
        'nextamountdue',
        'salesamount',
        'commissionpercent',
        'commissionamount',
        'specialsalesamount',
        'nextdate',
        'visible',
        'available',
        'fk_usercreator',
        'created_at',
        'fk_userupdater',
        'updated_at',
        'is_active'
    ];

    protected $casts = [
        'id' => 'integer',
        'fk_site' => 'integer',
        'fk_office' => 'integer',
        'fk_group' => 'integer',
        'reportdate' => 'timestamp',
        'amountdue' => 'float',
        'amountfailed' => 'float',
        'subtotalamount' => 'float',
        'amountrecovered' => 'float',
        'totalamount' => 'float',
        'incomingamount' => 'float',
        'outgoingamount' => 'float',
        'extraweekamount' => 'float',
        'totaldeliveryamount' => 'float',
        'nextamountdue' => 'float',
        'salesamount' => 'float',
        'commissionpercent' => 'float',
        'commissionamount' => 'float',
        'specialsalesamount' => 'float',
        'nextdate' => 'timestamp',
        'visible' => 'boolean',
        'available' => 'boolean',
        'fk_usercreator' => 'integer',
        'created_at' => 'timestamp',
        'fk_userupdater' => 'integer',
        'updated_at' => 'timestamp',
        'is_active' => 'boolean'
    ];


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

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

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

    public function office()
    {
        return $this->belongsTo(Office::class, 'fk_office', 'id');
    }

    public function group()
    {
        return $this->belongsTo(Group::class, 'fk_group', 'id')->select('id', 'fk_zone', 'groupname', 'fk_promoter')->where('is_active', true);
    }

    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');
    }
}
