<?php

namespace App\Models\User;

use CodeIgniter\Model;

/**
 * User Model
 * Handles database operations for users
 */
class UserModel extends Model
{
    protected $table = 'users';
    protected $primaryKey = 'UserID';

    // Allow all fields to be mass-assigned
    protected $allowedFields = [
        'Username',
        'Email',
        'Name',
        'Role',
        'Department',
        'IsActive',
        'CreatedAt',
        'UpdatedAt',
        'DelDate'
    ];

    // Use timestamps (disabled, we handle manually for consistency)
    protected $useTimestamps = false;

    // Validation rules
    protected $validationRules = [
        'Username' => 'required|min_length[3]|max_length[50]',
        'Email' => 'required|valid_email|max_length[100]',
    ];

    protected $validationMessages = [
        'Username' => [
            'required' => 'Username is required',
            'min_length' => 'Username must be at least 3 characters',
            'max_length' => 'Username cannot exceed 50 characters',
        ],
        'Email' => [
            'required' => 'Email is required',
            'valid_email' => 'Please provide a valid email address',
            'max_length' => 'Email cannot exceed 100 characters',
        ],
    ];

    /**
     * Get active users only
     */
    public function getActive()
    {
        return $this->where('DelDate', null)
            ->where('IsActive', true)
            ->findAll();
    }

    /**
     * Find user by username
     */
    public function findByUsername($username)
    {
        return $this->where('Username', $username)
            ->where('DelDate', null)
            ->first();
    }

    /**
     * Find user by email
     */
    public function findByEmail($email)
    {
        return $this->where('Email', $email)
            ->where('DelDate', null)
            ->first();
    }

    /**
     * Search users by name, username, or email
     */
    public function search($term)
    {
        return $this->where('DelDate', null)
            ->groupStart()
                ->like('Username', $term)
                ->orLike('Email', $term)
                ->orLike('Name', $term)
            ->groupEnd()
            ->findAll();
    }

    /**
     * Get users by role
     */
    public function getByRole($role)
    {
        return $this->where('Role', $role)
            ->where('DelDate', null)
            ->where('IsActive', true)
            ->findAll();
    }

    /**
     * Get users by department
     */
    public function getByDepartment($department)
    {
        return $this->where('Department', $department)
            ->where('DelDate', null)
            ->where('IsActive', true)
            ->findAll();
    }

    /**
     * Soft delete user
     */
    public function softDelete($id)
    {
        return $this->update($id, [
            'DelDate' => date('Y-m-d H:i:s'),
            'IsActive' => false
        ]);
    }

    /**
     * Restore soft-deleted user
     */
    public function restore($id)
    {
        return $this->update($id, [
            'DelDate' => null,
            'IsActive' => true
        ]);
    }
}