<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ActivityAssignmentRule extends Model
{
    use HasFactory, HasUuids;

    public $incrementing = false;
    protected $keyType = 'string';

    protected $fillable = [
        'company_id',
        'rule_type',
        'target_values',
        'frequency',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'target_values' => 'array',
            'is_active' => 'boolean',
        ];
    }

    /**
     * Get the company that owns the rule.
     */
    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    /**
     * Scope a query to only include active rules.
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope a query for a specific company.
     */
    public function scopeForCompany($query, $companyId)
    {
        return $query->where('company_id', $companyId);
    }

    /**
     * Check if the rule applies to a given user.
     */
    public function appliesToUser(User $user): bool
    {
        if (!$this->is_active) {
            return false;
        }

        return match ($this->rule_type) {
            'all_users' => true,
            'specific_users' => in_array($user->id, $this->target_values ?? []),
            'departments' => in_array($user->department, $this->target_values ?? []),
            'positions' => in_array($user->position, $this->target_values ?? []),
            default => false,
        };
    }

    /**
     * Get users that match this rule.
     */
    public function getMatchingUsers()
    {
        $query = User::where('company_id', $this->company_id)
            ->where('is_active', true);

        return match ($this->rule_type) {
            'all_users' => $query->get(),
            'specific_users' => $query->whereIn('id', $this->target_values ?? [])->get(),
            'departments' => $query->whereIn('department', $this->target_values ?? [])->get(),
            'positions' => $query->whereIn('position', $this->target_values ?? [])->get(),
            default => collect(),
        };
    }

    /**
     * Get a human-readable description of the rule.
     */
    public function getDescriptionAttribute(): string
    {
        return match ($this->rule_type) {
            'all_users' => 'Semua karyawan',
            'specific_users' => 'Karyawan tertentu (' . count($this->target_values ?? []) . ' orang)',
            'departments' => 'Departemen: ' . implode(', ', $this->target_values ?? []),
            'positions' => 'Jabatan: ' . implode(', ', $this->target_values ?? []),
            default => 'Unknown rule type',
        };
    }

    /**
     * Get rule type label in Indonesian.
     */
    public function getRuleTypeLabelAttribute(): string
    {
        return match ($this->rule_type) {
            'all_users' => 'Semua Karyawan',
            'specific_users' => 'Karyawan Tertentu',
            'departments' => 'Berdasarkan Departemen',
            'positions' => 'Berdasarkan Jabatan',
            default => $this->rule_type,
        };
    }
}
