<?php

namespace Modules\Settings\Models;

use CodeIgniter\Model;
use CodeIgniter\I18n\Time;

class ModuleSectionModel extends Model
{
    protected $table = 'module_sections';
    protected $primaryKey = 'id';
    protected $allowedFields = ['name', 'slug', 'description', 'icon', 'is_active', 'sort_order'];

    protected $returnType     = 'array';
    protected $useTimestamps  = true;
    protected $useSoftDeletes = true;
    
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    public function getAll()
    {
        $builder = $this->db->table('module_sections as ms');
        $builder->where('ms.deleted_at', null);
        $result = $builder->get()->getResult();
        return $result;
    }

    public function getSectionsWithPermissions()
    {
        $builder = $this->db->table('module_sections');
        $builder->select('module_sections.*, COUNT(p.id) as permission_count')
                ->join('permissions as p', 'p.module_section_id = module_sections.id', 'left')
                ->groupBy('module_sections.id')
                ->orderBy('sort_order', 'ASC');
        return $results = $builder->get()->getResult();
    }

}