<?php

namespace App\Models;

use CodeIgniter\Model;

class ChurchModel extends Model
{
    protected $table            = 'churches';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = true;
    protected $deletedField     = 'deleted_at';
    protected $useTimestamps    = true;
    protected $createdField     = 'created_at';
    protected $updatedField     = 'updated_at';

    protected $allowedFields = [
        'uuid', 'name', 'address', 'phone', 'email', 'website',
        'logo', 'parent_church_id', 'is_active',
    ];

    protected $validationRules = [
        'name'  => 'required|min_length[2]|max_length[255]',
        'email' => 'permit_empty|valid_email',
    ];

    public function findByUuid(string $uuid): ?array
    {
        return $this->where('uuid', $uuid)->where('deleted_at IS NULL')->first();
    }

    public function getBranches(int $parentId): array
    {
        return $this->where('parent_church_id', $parentId)->where('deleted_at IS NULL')->findAll();
    }
}
