<?php

namespace App\Models;

use CodeIgniter\Model;

class AuthorizedUserModel extends Model
{
    protected $table = 'authorized_users'; // Table name
    protected $primaryKey = 'id'; // Primary key of the table

    // Allowed fields for mass assignment
    protected $allowedFields = [
        'user_id',
        'authorized_user_id',
        'firstname',
        'lastname',
        'phone_number',
        'email',
        'relation_to_user',
        'token',
        'status',
        'created_at',
        'updated_at'
    ];

    protected $returnType = 'array'; // Return data as an array

    // Automatically manage created_at and updated_at fields
    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';

    // Validation rules
    protected $validationRules = [
        'email' => 'required|valid_email',
        'user_id' => 'required|integer',
        'firstname' => 'required|string|max_length[255]',
        'lastname' => 'required|string|max_length[255]',
        'phone_number' => 'required|string|max_length[25]',
        'relation_to_user' => 'string|max_length[64]',
        'status' => 'required|in_list[Pending,Active]',
    ];

    // Custom validation messages
    protected $validationMessages = [
        'email' => [
            'required' => 'Email is required',
            'valid_email' => 'Email must be valid',
        ],
        'user_id' => [
            'required' => 'User ID is required',
            'integer' => 'User ID must be an integer',
        ],
        'firstname' => [
            'required' => 'First name is required',
            'string' => 'First name must be a string',
            'max_length' => 'First name cannot exceed 255 characters',
        ],
        'lastname' => [
            'required' => 'Last name is required',
            'string' => 'Last name must be a string',
            'max_length' => 'Last name cannot exceed 255 characters',
        ],
        'phone_number' => [
            'required' => 'Phone number is required',
            'string' => 'Phone number must be a string',
            'max_length' => 'Phone number cannot exceed 25 characters',
        ],
        'relation_to_user' => [
            'string' => 'Relation to user must be a string',
            'max_length' => 'Relation to user cannot exceed 64 characters',
        ],
        'status' => [
            'required' => 'Status is required',
            'in_list' => 'Status must be either Pending or Active',
        ],
    ];

    // Optional: Skip validation for bulk inserts
    protected $skipValidation = false;
}
