<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Employee extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'name',
        'email',
        'phone',
        'password',
        'role',
        'sub_role',
        'restaurant_id',
        'branch_id',
        'is_active',
        'enable_login',
        'salary_type',
        'current_salary_amount',
        'user_id',
    ];

    protected $hidden = [
        'password',
    ];

    protected $casts = [
        'is_active' => 'boolean',
        'enable_login' => 'boolean',
        'current_salary_amount' => 'decimal:2',
    ];

    /**
     * Get the restaurant that owns the employee
     */
    public function restaurant()
    {
        return $this->belongsTo(Restaurant::class, 'restaurant_id');
    }

    /**
     * Get the branch that owns the employee
     */
    public function branch()
    {
        return $this->belongsTo(Branch::class, 'branch_id');
    }

    /**
     * Get the user that this employee is linked to
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * Get the employee's salaries
     */
    public function salaries()
    {
        return $this->hasMany(EmployeeSalary::class, 'employee_id');
    }

    /**
     * Get the employee's wallet transactions
     */
    public function walletTransactions()
    {
        return $this->hasMany(EmployeeWalletTransaction::class, 'employee_id');
    }

    /**
     * Get the waiter record if this employee is a waiter
     */
    public function waiter()
    {
        return $this->hasOne(Waiter::class, 'employee_id');
    }

    /**
     * Scope to filter by restaurant
     */
    public function scopeForRestaurant($query, $restaurantId)
    {
        return $query->where('restaurant_id', $restaurantId);
    }

    /**
     * Scope to filter by branch
     */
    public function scopeForBranch($query, $branchId)
    {
        return $query->where('branch_id', $branchId);
    }

    /**
     * Scope to filter active employees
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope to filter by role
     */
    public function scopeByRole($query, $role)
    {
        return $query->where('role', $role);
    }

    /**
     * Get the current wallet balance (credits - debits)
     */
    public function getWalletBalance()
    {
        $credits = $this->walletTransactions()->credits()->sum('amount') ?? 0;
        $debits = $this->walletTransactions()->debits()->sum('amount') ?? 0;
        return $credits - $debits;
    }

    /**
     * Get the current salary amount based on salary type
     */
    public function getCurrentSalaryAmount()
    {
        if ($this->current_salary_amount) {
            return $this->current_salary_amount;
        }

        // Fallback: Get the latest salary record amount
        $latestSalary = $this->salaries()
            ->where('status', 'paid')
            ->orderBy('created_at', 'desc')
            ->first();

        return $latestSalary ? $latestSalary->amount : 0;
    }
}
