<?php

namespace App\Models;

use CodeIgniter\Model;

class OrganizationPrivacySettingModel extends Model
{
    protected $table         = 'organization_privacy_settings';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = true;
    protected $allowedFields = [
        'organization_id',
        'visibility_mode',
        'show_public_reference',
        'show_organization_name',
        'show_facility_name',
        'show_energy',
        'show_period',
        'show_blockchain',
        'show_hashes',
        'show_rule_name',
    ];

    public function defaultSettings(int $organizationId): array
    {
        return [
            'organization_id' => $organizationId,
            'visibility_mode' => 'standard',
            'show_public_reference' => 1,
            'show_organization_name' => 1,
            'show_facility_name' => 0,
            'show_energy' => 1,
            'show_period' => 1,
            'show_blockchain' => 0,
            'show_hashes' => 1,
            'show_rule_name' => 0,
        ];
    }

    public function forOrganization(int $organizationId): array
    {
        $settings = $this->where('organization_id', $organizationId)->first();

        return $settings ?: $this->defaultSettings($organizationId);
    }
}
