<?php

namespace GemBoards\Settings;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Base {

	public static function get_settings_saved_data() {
		$settings = get_option( GEMBOARDS_SETTINGS_NAME );
		if ( $settings ) {
			return json_decode( $settings, true );
		}

		return [];
	}

	public static function get_settings_default_data() {
		return apply_filters( 'gemboards/admin/settings_default_data', [
			'title'                 => 'GemBoards',
			'path'                  => 'workspace',
			'logo'                  => null,
			'load_on_entire_site'   => false,
			'notification_position' => 'top-right',
			'permissions'           => [
				'update_projects' => [
					'member'  => false,
					'manager' => true,
				],
				'view_projects'   => [
					'member'  => true,
					'manager' => true,
				],
				'manage_tasks'    => [
					'member'  => true,
					'manager' => true,
				],
				'manage_stages'   => [
					'member'  => false,
					'manager' => true,
				],
				'allow_comments'  => [
					'member'  => true,
					'manager' => true,
				],
				'mention_user'    => [
					'member'  => true,
					'manager' => true,
				],
			],
			'notifications'         => [
				'assign_tasks'   => [
					'gemboards' => true,
					'email'     => true,
				],
				'unassign_tasks' => [
					'gemboards' => true,
					'email'     => true,
				],
				'stage_changes'  => [
					'gemboards' => true,
					'email'     => true,
				],
				'new_comment'    => [
					'gemboards' => true,
					'email'     => true,
				],
			],
		] );
	}

	public static function save_settings( array $form_data = [] ): bool {
		$default_data  = self::get_settings_default_data();
		$saved_data    = self::get_settings_saved_data();
		$settings_data = array_replace_recursive( $default_data, $saved_data );
		if ( $form_data ) {
			$settings_data = wp_parse_args( $form_data, $settings_data );
		}

		return update_option( GEMBOARDS_SETTINGS_NAME, wp_json_encode( $settings_data ) );
	}
}
