<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

namespace local_kid\local;

defined('MOODLE_INTERNAL') || die();

/**
 * Sole write path for `local_kid_dashboard_favori` (Comparateur de bilans
 * saved filter sets). Every method is strictly scoped to the calling
 * `$userid` — a favori belongs to exactly one user, never shared, never
 * visible to another user even indirectly. Sesskey validation is the
 * caller's responsibility (page controller / external function), not this
 * repository's — consistent with the rest of the plugin.
 */
class dashboard_favori_repository {

    public static function get_for_user(int $userid): array {
        global $DB;
        return $DB->get_records('local_kid_dashboard_favori', ['userid' => $userid], 'name ASC');
    }

    public static function get(int $id): ?\stdClass {
        global $DB;
        return $DB->get_record('local_kid_dashboard_favori', ['id' => $id], '*', IGNORE_MISSING) ?: null;
    }

    /**
     * @param array $config already-sanitised filter set (promotionid,
     *   studentids, sourcekeys, competencyids, niveaucodes, showcomments,
     *   onlyobserved) — serialised as-is, this repository doesn't interpret it.
     */
    public static function create(int $userid, string $name, array $config): int {
        global $DB;
        $transaction = $DB->start_delegated_transaction();
        $now = time();
        $id = $DB->insert_record('local_kid_dashboard_favori', (object) [
            'userid' => $userid,
            'name' => $name,
            'config' => json_encode($config),
            'timecreated' => $now,
            'timemodified' => $now,
        ]);
        $transaction->allow_commit();
        return $id;
    }

    /** @return bool false if the favori doesn't exist or isn't owned by $userid. */
    public static function rename(int $id, int $userid, string $name): bool {
        global $DB;
        $favori = self::get($id);
        if (!$favori || (int) $favori->userid !== $userid) {
            return false;
        }
        $transaction = $DB->start_delegated_transaction();
        $DB->update_record('local_kid_dashboard_favori', (object) [
            'id' => $id,
            'name' => $name,
            'timemodified' => time(),
        ]);
        $transaction->allow_commit();
        return true;
    }

    /** @return bool false if the favori doesn't exist or isn't owned by $userid. */
    public static function delete(int $id, int $userid): bool {
        global $DB;
        $favori = self::get($id);
        if (!$favori || (int) $favori->userid !== $userid) {
            return false;
        }
        $transaction = $DB->start_delegated_transaction();
        $DB->delete_records('local_kid_dashboard_favori', ['id' => $id]);
        $transaction->allow_commit();
        return true;
    }
}
