<?php

namespace App\Models;

use Carbon\Carbon;
use App\Models\Scopes\ModelScopes;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Validator;

class User extends Authenticatable
{
    use ModelScopes;

    protected $table = 'users';

    protected $hidden = [
        'password',
        'remember_token'
    ];

    protected $fillable = [
        'role_id',
        'nid',
        'otp',
        'name',
        'company_name',
        'contact_number',
        'address',
        'username',
        'email',
        'password',
        'api_key',
        'default_sender_id',
        'is_superadmin',
        'masking_rate',
        'non_masking_rate',
        'isAllowNegetiveBalance',
        'negativeLimit',
        'theme_mode',
        'locale',
        'status',
    ];

    protected $casts = [
        'is_superadmin' => 'integer',
        'status' => 'integer',
        'masking_rate' => 'float',
        'non_masking_rate' => 'float',
        'isAllowNegetiveBalance' => 'integer',
        'negativeLimit' => 'float',
        'created_at' => 'datetime:d-m-Y',
    ];

    public function validate($input)
    {
        return Validator::make($input, [
            'nid' => 'required',
            'name' => 'required',
            'company_name' => 'required',
            'contact_number' => 'required',
            'address' => 'required',
            'username' => 'required',
            'email' => 'required|email',
            'password' => 'required',
        ]);
    }

    public function roles(): BelongsTo
    {
        return $this->belongsTo(Role::class, 'role_id', 'id');
    }

    public function default_sender(): BelongsTo
    {
        return $this->belongsTo(SenderIdRequest::class, 'default_sender_id', 'id');
    }

    public function smsCounts(): HasMany
    {
        return $this->hasMany(SmsCount::class);
    }

    public function userPackages(): HasMany
    {
        return $this->hasMany(UserPackage::class, 'user_id');
    }

    public function payments(): HasMany
    {
        return $this->hasMany(Payment::class, 'user_id');
    }

    public function userDevices(): HasMany
    {
        return $this->hasMany(UserDevice::class, 'user_id');
    }

    public function smsMessages(): HasMany
    {
        return $this->hasMany(SmsMessage::class, 'user_id');
    }

    /**
     * Get currently active user package subscription if not expired.
     */
    public function activePackage(): ?UserPackage
    {
        $pkg = UserPackage::where('user_id', $this->id)
            ->where('status', UserPackage::STATUS_ACTIVE)
            ->latest('id')
            ->first();

        if ($pkg && $pkg->is_expired) {
            $pkg->status = UserPackage::STATUS_INACTIVE;
            $pkg->save();
            return null;
        }

        if ($pkg) {
            $pkg->checkAndResetDailySent();
        }

        return $pkg;
    }
}
