<link rel="stylesheet" href="/public/assets/css/axiom-studio.css">
<?php
$h = static fn(mixed $value): string => htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
$moduleId = (int)$module['id'];
$selectedId = $selectedEntity ? (int)$selectedEntity['id'] : 0;
$typeLabels = [
 'string'=>'Texte','text'=>'Texte long','integer'=>'Entier','bigint'=>'Grand entier','decimal'=>'Décimal','boolean'=>'Booléen',
 'date'=>'Date','time'=>'Heure','datetime'=>'Date et heure','email'=>'E-mail','phone'=>'Téléphone','url'=>'URL','enum'=>'Liste / Enum',
 'json'=>'JSON','address'=>'Adresse','gps'=>'Coordonnées GPS','image'=>'Image','pdf'=>'PDF','file'=>'Fichier','signature'=>'Signature'
];
?>
<main class="axiom-studio studio-designer-page">
<section class="axiom-studio-hero studio-designer-hero">
  <div><span class="axiom-studio-kicker">ENTITY DESIGNER 1.0</span><h2><?= $h($module['name']) ?></h2><p>Modélisez les entités, champs et relations sans écrire de SQL.</p></div>
  <div class="studio-actions"><a href="/public/administration/axiom/modules">← Modules</a><span class="module-version">v<?= $h($module['version']) ?></span></div>
</section>
<?php if (!empty($_SESSION['flash_success'])): ?><div class="studio-alert success"><?= $h($_SESSION['flash_success']); unset($_SESSION['flash_success']); ?></div><?php endif; ?>
<?php if (!empty($_SESSION['flash_error'])): ?><div class="studio-alert error"><?= $h($_SESSION['flash_error']); unset($_SESSION['flash_error']); ?></div><?php endif; ?>

<div class="studio-workbench">
  <aside class="studio-panel studio-tree">
    <div class="studio-panel-head"><div><span class="axiom-studio-kicker">EXPLORATEUR</span><h3>Entités</h3></div><button type="button" class="studio-icon-button" data-dialog="entity-dialog" aria-label="Nouvelle entité">＋</button></div>
    <nav class="studio-entity-list">
      <?php if (!$entities): ?><p class="studio-empty">Aucune entité. Commencez par créer votre première table métier.</p><?php endif; ?>
      <?php foreach ($entities as $entity): ?>
      <a class="<?= (int)$entity['id'] === $selectedId ? 'active' : '' ?>" href="/public/administration/axiom/entity-designer?module_id=<?= $moduleId ?>&entity_id=<?= (int)$entity['id'] ?>">
        <span>▦</span><div><strong><?= $h($entity['name']) ?></strong><small><?= $h($entity['table_name']) ?></small></div>
      </a>
      <?php endforeach; ?>
    </nav>
    <button type="button" class="studio-secondary wide" data-dialog="relation-dialog" <?= count($entities) < 2 ? 'disabled title="Créez au moins deux entités"' : '' ?>>＋ Nouvelle relation</button>
  </aside>

  <section class="studio-panel studio-canvas">
    <?php if ($selectedEntity): ?>
    <div class="studio-panel-head"><div><span class="axiom-studio-kicker">ENTITÉ</span><h3><?= $h($selectedEntity['name']) ?></h3><code><?= $h($selectedEntity['table_name']) ?></code></div><button type="button" class="studio-primary" data-dialog="field-dialog">＋ Ajouter un champ</button></div>
    <?php if ($selectedEntity['description']): ?><p class="studio-muted"><?= $h($selectedEntity['description']) ?></p><?php endif; ?>
    <div class="studio-field-table-wrap"><table class="studio-field-table"><thead><tr><th>Champ</th><th>Type</th><th>Contraintes</th><th>Défaut</th><th></th></tr></thead><tbody>
      <tr class="system-field"><td><strong>id</strong><small>Identifiant système</small></td><td><span class="type-chip">BIGINT</span></td><td><span class="constraint-chip">PK</span><span class="constraint-chip">AUTO</span></td><td>—</td><td></td></tr>
      <?php foreach ($fields as $field): ?>
      <tr><td><strong><?= $h($field['label']) ?></strong><small><?= $h($field['code']) ?><?= $field['help_text'] ? ' · '.$h($field['help_text']) : '' ?></small></td><td><span class="type-chip"><?= $h($typeLabels[$field['field_type']] ?? $field['field_type']) ?></span></td><td><?php if ($field['is_required']): ?><span class="constraint-chip">REQUIS</span><?php endif; ?><?php if ($field['is_unique']): ?><span class="constraint-chip">UNIQUE</span><?php endif; ?><?php if ($field['is_indexed']): ?><span class="constraint-chip">INDEX</span><?php endif; ?></td><td><?= $field['default_value'] !== null ? $h($field['default_value']) : '—' ?></td><td><form method="post" action="/public/administration/axiom/entity-designer/archiver" onsubmit="return confirm('Archiver ce champ ?')"><?= \App\Core\Csrf::field() ?><input type="hidden" name="module_id" value="<?= $moduleId ?>"><input type="hidden" name="entity_id" value="<?= $selectedId ?>"><input type="hidden" name="object_type" value="field"><input type="hidden" name="object_id" value="<?= (int)$field['id'] ?>"><button class="studio-row-action" aria-label="Archiver">×</button></form></td></tr>
      <?php endforeach; ?>
      <?php if (!$fields): ?><tr><td colspan="5" class="studio-empty-cell">Aucun champ métier. L’identifiant et les dates techniques seront générés automatiquement.</td></tr><?php endif; ?>
    </tbody></table></div>
    <div class="studio-relations-inline"><h4>Relations du module</h4><?php if (!$relations): ?><p class="studio-muted">Aucune relation définie.</p><?php endif; ?><?php foreach ($relations as $relation): ?><article><span><?= $h($relation['source_name']) ?></span><strong><?= $h(str_replace('_', ' → ', $relation['relation_type'])) ?></strong><span><?= $h($relation['target_name']) ?></span><code><?= $h($relation['source_field']) ?> → <?= $h($relation['target_field']) ?></code><form method="post" action="/public/administration/axiom/entity-designer/archiver" onsubmit="return confirm('Archiver cette relation ?')"><?= \App\Core\Csrf::field() ?><input type="hidden" name="module_id" value="<?= $moduleId ?>"><input type="hidden" name="entity_id" value="<?= $selectedId ?>"><input type="hidden" name="object_type" value="relation"><input type="hidden" name="object_id" value="<?= (int)$relation['id'] ?>"><button class="studio-row-action">×</button></form></article><?php endforeach; ?></div>
    <?php else: ?><div class="studio-canvas-empty"><div>▦</div><h3>Créez votre première entité</h3><p>Une entité représente une table métier : Salle, Réservation, Adhérent, Facture…</p><button type="button" class="studio-primary" data-dialog="entity-dialog">Créer une entité</button></div><?php endif; ?>
  </section>

  <aside class="studio-panel studio-preview-panel">
    <div class="studio-panel-head"><div><span class="axiom-studio-kicker">APERÇU TEMPS RÉEL</span><h3>Génération</h3></div></div>
    <div class="studio-tabs"><button class="active" data-tab="sql">SQL</button><button data-tab="json">JSON</button><button data-tab="form">Formulaire</button></div>
    <div class="studio-tab active" data-tab-panel="sql"><pre><code><?= $h($preview['sql']) ?></code></pre></div>
    <div class="studio-tab" data-tab-panel="json"><pre><code><?= $h($preview['json']) ?></code></pre></div>
    <div class="studio-tab" data-tab-panel="form"><?php if (!$preview['form']): ?><p class="studio-empty">L’aperçu du formulaire apparaîtra ici.</p><?php endif; ?><?php foreach ($preview['form'] as $formEntity): ?><div class="studio-form-preview"><h4><?= $h($formEntity['entity']['name']) ?></h4><?php foreach ($formEntity['fields'] as $field): ?><label><?= $h($field['label']) ?><?php if ($field['is_required']): ?> *<?php endif; ?><input disabled placeholder="<?= $h($typeLabels[$field['field_type']] ?? $field['field_type']) ?>"></label><?php endforeach; ?></div><?php endforeach; ?></div>
  </aside>
</div>

<dialog id="entity-dialog" class="studio-dialog"><form method="post" action="/public/administration/axiom/entity-designer/entite"><?= \App\Core\Csrf::field() ?><input type="hidden" name="module_id" value="<?= $moduleId ?>"><div class="studio-dialog-head"><div><span class="axiom-studio-kicker">NOUVELLE ENTITÉ</span><h3>Ajouter une table métier</h3></div><button type="button" data-close>×</button></div><div class="studio-form-grid"><label>Nom affiché<input name="name" required placeholder="Salle" data-slug-source></label><label>Code technique<input name="code" required placeholder="salle" data-slug-target></label><label>Nom de table<input name="table_name" required placeholder="salles" data-table-target></label></div><label>Description<textarea name="description" rows="3" placeholder="Décrivez le rôle de cette entité"></textarea></label><div class="studio-actions"><button type="button" data-close>Annuler</button><button class="studio-primary" type="submit">Créer l’entité</button></div></form></dialog>

<dialog id="field-dialog" class="studio-dialog"><form method="post" action="/public/administration/axiom/entity-designer/champ"><?= \App\Core\Csrf::field() ?><input type="hidden" name="module_id" value="<?= $moduleId ?>"><input type="hidden" name="entity_id" value="<?= $selectedId ?>"><div class="studio-dialog-head"><div><span class="axiom-studio-kicker">NOUVEAU CHAMP</span><h3><?= $selectedEntity ? $h($selectedEntity['name']) : '' ?></h3></div><button type="button" data-close>×</button></div><div class="studio-form-grid"><label>Libellé<input name="label" required placeholder="Capacité" data-field-source></label><label>Code technique<input name="code" required placeholder="capacite" data-field-target></label><label>Type<select name="field_type"><?php foreach ($fieldTypes as $type): ?><option value="<?= $h($type) ?>"><?= $h($typeLabels[$type] ?? $type) ?></option><?php endforeach; ?></select></label><label>Longueur<input type="number" min="1" max="65535" name="length_value" placeholder="255"></label><label>Précision<input type="number" min="1" max="65" name="precision_value" placeholder="12"></label><label>Décimales<input type="number" min="0" max="30" name="scale_value" placeholder="2"></label><label>Valeur par défaut<input name="default_value"></label><label>Options (enum)<input name="options" placeholder="Brouillon, Publié, Archivé"></label></div><div class="studio-check-grid"><label><input type="checkbox" name="is_required" value="1"> Obligatoire</label><label><input type="checkbox" name="is_unique" value="1"> Unique</label><label><input type="checkbox" name="is_indexed" value="1"> Indexé</label></div><label>Aide utilisateur<input name="help_text" placeholder="Texte affiché sous le champ"></label><div class="studio-actions"><button type="button" data-close>Annuler</button><button class="studio-primary" type="submit">Ajouter le champ</button></div></form></dialog>

<dialog id="relation-dialog" class="studio-dialog"><form method="post" action="/public/administration/axiom/entity-designer/relation"><?= \App\Core\Csrf::field() ?><input type="hidden" name="module_id" value="<?= $moduleId ?>"><div class="studio-dialog-head"><div><span class="axiom-studio-kicker">RELATION DESIGNER</span><h3>Relier deux entités</h3></div><button type="button" data-close>×</button></div><div class="studio-form-grid"><label>Entité source<select name="source_entity_id" required><?php foreach ($entities as $entity): ?><option value="<?= (int)$entity['id'] ?>"><?= $h($entity['name']) ?></option><?php endforeach; ?></select></label><label>Type<select name="relation_type"><option value="many_to_one">N vers 1</option><option value="one_to_many">1 vers N</option><option value="one_to_one">1 vers 1</option><option value="many_to_many">N vers N</option></select></label><label>Entité cible<select name="target_entity_id" required><?php foreach ($entities as $entity): ?><option value="<?= (int)$entity['id'] ?>"><?= $h($entity['name']) ?></option><?php endforeach; ?></select></label><label>Champ source<input name="source_field" placeholder="salle_id"></label><label>Champ cible<input name="target_field" value="id"></label><label>Suppression<select name="on_delete"><option>RESTRICT</option><option>CASCADE</option><option>SET NULL</option><option>NO ACTION</option></select></label></div><label>Libellé<input name="label" placeholder="Réservation de salle"></label><div class="studio-actions"><button type="button" data-close>Annuler</button><button class="studio-primary" type="submit">Créer la relation</button></div></form></dialog>
</main>
<script src="/public/assets/js/axiom-entity-designer.js"></script>
