package Mojolicious::Plugin::Fondation::Menu;
$Mojolicious::Plugin::Fondation::Menu::VERSION = '0.04';
# ABSTRACT: Dynamic menu management plugin for Fondation — navbar, breadcrumb

use Mojo::Base 'Mojolicious::Plugin', -signatures;

sub fondation_meta {
    return {
        dependencies => [
            'Fondation::Model::DBIx::Async',
            'Fondation::MigrationDBIx',
            'Fondation::Layout::Bootstrap',
        ],
        defaults => {
            title  => 'Menu Management',
            models => {
                menu => { source => 'Menu', backend => undef },
            },
            fondation_init    => [
                ['menu', 'sync'],
            ],
            fondation_upgrade => [
                ['menu', 'sync'],
            ],
        },
    };
}

sub register ($self, $app, $conf) {

    # ── Register command namespace ───────────────────────────────────
    push @{$app->commands->namespaces},
        'Mojolicious::Plugin::Fondation::Menu::Command';

    # ── Helpers ──────────────────────────────────────────────────────
    require Mojolicious::Plugin::Fondation::Menu::Helpers;
    Mojolicious::Plugin::Fondation::Menu::Helpers->register_all($app);

    # ── Admin route (HTML) ───────────────────────────────────────────
    $app->routes->get('/menus')
        ->requires('fondation.perm' => 'menu_read')
        ->to(cb => sub ($c) {
            $c->render(template => 'menu/admin');
        });

    return $self;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Mojolicious::Plugin::Fondation::Menu - Dynamic menu management plugin for Fondation — navbar, breadcrumb

=head1 VERSION

version 0.04

=head1 SYNOPSIS

  # myapp.conf
  {
      'Fondation' => {
          dependencies => [
              { 'Fondation::Model::DBIx::Async' => {
                  backends => [ main => {
                      dsn          => 'dbi:SQLite:dbname=data/app.db',
                      schema_class => 'MySchema',
                  }],
              }},
              'Fondation::Menu',
          ],
      },
  }

=head1 DESCRIPTION

L<Mojolicious::Plugin::Fondation::Menu> provides dynamic menu management.
Menus are declared declaratively via C<share/menus.json> in each plugin
and synchronized with the database via C<menu sync>.

=head2 Schema

  id          INTEGER PRIMARY KEY AUTOINCREMENT
  title       TEXT NOT NULL
  link        TEXT
  icon        TEXT          (FontAwesome class)
  icon_color  TEXT          (CSS color)
  name        TEXT NOT NULL DEFAULT 'left'  (menu group)
  condition   TEXT          ("group:NAME", "perm:NAME", or "")
  sort_order  INTEGER DEFAULT 0
  parent_id   INTEGER NOT NULL DEFAULT 0
  open_tab    INTEGER DEFAULT 0
  view_in_menu INTEGER DEFAULT 1
  description TEXT
  created_at  TEXT
  updated_at  TEXT

=head2 menus.json format

Each plugin can ship a C<share/menus.json>:

  [
    {
      "title":        "Administration",
      "link":         "/admin",
      "icon":         "fas fa-lock-open",
      "menu":         "admin_menu",
      "condition":    "group:administrateur",
      "order":        1,
      "parent_title": null,
      "description":  "Admin panel"
    },
    {
      "title":        "Users",
      "link":         "/users",
      "menu":         "admin_menu",
      "parent_title": "Administration"
    }
  ]

Fields: C<title> (required), C<link>, C<icon>, C<icon_color>,
C<menu> (group name, default "left"), C<condition>, C<order>
(auto if absent), C<parent_title>, C<open_tab>, C<view_in_menu>,
C<description>.

=head2 Commands

  myapp.pl menu sync       Sync menu entries from share/menus.json

=head2 Helpers

  $c->menus
  $c->menu_by_name('left_menu')
  $c->menu_by_id(42)
  $c->breadcrumb
  $c->check_menu_condition('group:admin')
  $c->render_menu('left_menu')
  $c->render_menu_breadcrumb

=head2 REST API (auto-generated by OpenAPI)

  GET    /api/menu
  POST   /api/menu
  GET    /api/menu/:id
  PUT    /api/menu/:id
  DELETE /api/menu/:id

=head2 Permissions

  menu_read    menu_create    menu_update    menu_delete

=head1 AUTHOR

Daniel Brosseau <dab@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Daniel Brosseau.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
