<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20260304173000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add comments on all database tables and columns';
    }

    public function up(Schema $schema): void
    {
        // absence_types
        $this->addSql("COMMENT ON TABLE absence_types IS 'Catalogue des types d absence (CP, RTT, maladie, etc.).'");
        $this->addSql("COMMENT ON COLUMN absence_types.id IS 'Identifiant technique du type d absence.'");
        $this->addSql("COMMENT ON COLUMN absence_types.code IS 'Code metier court du type d absence.'");
        $this->addSql("COMMENT ON COLUMN absence_types.label IS 'Libelle metier du type d absence.'");
        $this->addSql("COMMENT ON COLUMN absence_types.color IS 'Couleur d affichage du type d absence.'");
        $this->addSql("COMMENT ON COLUMN absence_types.count_as_worked_hours IS 'Indique si l absence est comptee comme temps travaille.'");

        // absences
        $this->addSql("COMMENT ON TABLE absences IS 'Absences employees enregistrees par jour et demi journee.'");
        $this->addSql("COMMENT ON COLUMN absences.id IS 'Identifiant technique de l absence.'");
        $this->addSql("COMMENT ON COLUMN absences.employee_id IS 'Employe concerne par l absence.'");
        $this->addSql("COMMENT ON COLUMN absences.type_id IS 'Type d absence applique.'");
        $this->addSql("COMMENT ON COLUMN absences.comment IS 'Commentaire libre de l absence.'");
        $this->addSql("COMMENT ON COLUMN absences.start_date IS 'Date de debut de l absence.'");
        $this->addSql("COMMENT ON COLUMN absences.end_date IS 'Date de fin de l absence.'");
        $this->addSql("COMMENT ON COLUMN absences.start_half IS 'Demi journee de debut (AM ou PM).'");
        $this->addSql("COMMENT ON COLUMN absences.end_half IS 'Demi journee de fin (AM ou PM).'");

        // contracts
        $this->addSql("COMMENT ON TABLE contracts IS 'Referentiel des contrats de travail utilisables.'");
        $this->addSql("COMMENT ON COLUMN contracts.id IS 'Identifiant technique du contrat.'");
        $this->addSql("COMMENT ON COLUMN contracts.name IS 'Nom metier du contrat (35h, 39h, Forfait, etc.).'");
        $this->addSql("COMMENT ON COLUMN contracts.tracking_mode IS 'Mode de suivi du contrat (TIME ou PRESENCE).'");
        $this->addSql("COMMENT ON COLUMN contracts.weekly_hours IS 'Volume horaire hebdomadaire contractuel, si applicable.'");
        $this->addSql("COMMENT ON COLUMN contracts.is_active IS 'Indique si le contrat est actif dans le referentiel.'");

        // doctrine_migration_versions
        $this->addSql("COMMENT ON TABLE doctrine_migration_versions IS 'Historique technique des migrations Doctrine appliquees.'");
        $this->addSql("COMMENT ON COLUMN doctrine_migration_versions.version IS 'Version unique de migration appliquee.'");
        $this->addSql("COMMENT ON COLUMN doctrine_migration_versions.executed_at IS 'Date et heure d execution de la migration.'");
        $this->addSql("COMMENT ON COLUMN doctrine_migration_versions.execution_time IS 'Duree d execution de la migration en millisecondes.'");

        // employee_contract_periods
        $this->addSql("COMMENT ON TABLE employee_contract_periods IS 'Historique des periodes de contrat par employe.'");
        $this->addSql("COMMENT ON COLUMN employee_contract_periods.id IS 'Identifiant technique de la periode contrat.'");
        $this->addSql("COMMENT ON COLUMN employee_contract_periods.employee_id IS 'Employe concerne par la periode contrat.'");
        $this->addSql("COMMENT ON COLUMN employee_contract_periods.contract_id IS 'Contrat applique sur la periode.'");
        $this->addSql("COMMENT ON COLUMN employee_contract_periods.start_date IS 'Date de debut de la periode contrat.'");
        $this->addSql("COMMENT ON COLUMN employee_contract_periods.end_date IS 'Date de fin de la periode contrat, null si en cours.'");
        $this->addSql("COMMENT ON COLUMN employee_contract_periods.created_at IS 'Date de creation technique de la periode contrat.'");
        $this->addSql("COMMENT ON COLUMN employee_contract_periods.contract_nature IS 'Nature du contrat (CDI, CDD, INTERIM).'");
        $this->addSql("COMMENT ON COLUMN employee_contract_periods.paid_leave_settled IS 'Indique si les conges ont ete soldes a la cloture de cette periode.'");

        // employee_leave_balances
        $this->addSql("COMMENT ON TABLE employee_leave_balances IS 'Soldes de conges par employe et exercice (ouverture, mouvements, cloture).'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.id IS 'Identifiant technique du solde annuel.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.employee_id IS 'Employe concerne par le solde annuel.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.rule_code IS 'Code de regle de calcul des conges (CDI_CDD_NON_FORFAIT, FORFAIT_218, ...).'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.year IS 'Annee d exercice de reference (ex: 2026).'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.opening_days IS 'Report N-1 en jours a l ouverture de l exercice.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.opening_saturdays IS 'Report N-1 en samedis a l ouverture (0 pour forfait).'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.accrued_days IS 'Droits jours acquis sur l exercice courant.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.accrued_saturdays IS 'Droits samedis acquis sur l exercice courant.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.taken_days IS 'Jours de conges consommes sur l exercice.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.taken_saturdays IS 'Samedis consommes sur l exercice.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.closing_days IS 'Solde de cloture jours sur l exercice.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.closing_saturdays IS 'Solde de cloture samedis sur l exercice.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.is_locked IS 'Indique si le solde de l exercice est fige (verrouille RH).'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.created_at IS 'Date de creation technique du solde.'");
        $this->addSql("COMMENT ON COLUMN employee_leave_balances.updated_at IS 'Date de derniere mise a jour du solde.'");

        // employees
        $this->addSql("COMMENT ON TABLE employees IS 'Referentiel des employes.'");
        $this->addSql("COMMENT ON COLUMN employees.id IS 'Identifiant technique de l employe.'");
        $this->addSql("COMMENT ON COLUMN employees.first_name IS 'Prenom de l employe.'");
        $this->addSql("COMMENT ON COLUMN employees.last_name IS 'Nom de l employe.'");
        $this->addSql("COMMENT ON COLUMN employees.created_at IS 'Date de creation technique de l employe.'");
        $this->addSql("COMMENT ON COLUMN employees.site_id IS 'Site principal de rattachement de l employe.'");
        $this->addSql("COMMENT ON COLUMN employees.display_order IS 'Ordre d affichage de l employe dans les listes UI.'");
        $this->addSql("COMMENT ON COLUMN employees.contract_id IS 'Contrat courant reference sur la fiche employe.'");

        // notifications
        $this->addSql("COMMENT ON TABLE notifications IS 'Notifications applicatives envoyees aux utilisateurs.'");
        $this->addSql("COMMENT ON COLUMN notifications.id IS 'Identifiant technique de la notification.'");
        $this->addSql("COMMENT ON COLUMN notifications.recipient_id IS 'Utilisateur destinataire de la notification.'");
        $this->addSql("COMMENT ON COLUMN notifications.title IS 'Titre court de la notification.'");
        $this->addSql("COMMENT ON COLUMN notifications.message IS 'Message detaille de la notification.'");
        $this->addSql("COMMENT ON COLUMN notifications.is_read IS 'Indique si la notification a ete lue.'");
        $this->addSql("COMMENT ON COLUMN notifications.created_at IS 'Date de creation de la notification.'");

        // sites
        $this->addSql("COMMENT ON TABLE sites IS 'Referentiel des sites de l entreprise.'");
        $this->addSql("COMMENT ON COLUMN sites.id IS 'Identifiant technique du site.'");
        $this->addSql("COMMENT ON COLUMN sites.name IS 'Nom du site.'");
        $this->addSql("COMMENT ON COLUMN sites.color IS 'Couleur associee au site pour affichage UI.'");
        $this->addSql("COMMENT ON COLUMN sites.display_order IS 'Ordre d affichage du site dans les listes UI.'");

        // user_site_roles
        $this->addSql("COMMENT ON TABLE user_site_roles IS 'Roles attribues aux utilisateurs sur des sites donnes.'");
        $this->addSql("COMMENT ON COLUMN user_site_roles.id IS 'Identifiant technique de l attribution de role site.'");
        $this->addSql("COMMENT ON COLUMN user_site_roles.user_id IS 'Utilisateur concerne par l attribution.'");
        $this->addSql("COMMENT ON COLUMN user_site_roles.site_id IS 'Site concerne par l attribution.'");
        $this->addSql("COMMENT ON COLUMN user_site_roles.role IS 'Role attribue sur le site (ex: ROLE_SITE_MANAGER).'");

        // users
        $this->addSql("COMMENT ON TABLE users IS 'Comptes utilisateurs de l application.'");
        $this->addSql("COMMENT ON COLUMN users.id IS 'Identifiant technique du compte utilisateur.'");
        $this->addSql("COMMENT ON COLUMN users.username IS 'Identifiant de connexion de l utilisateur.'");
        $this->addSql("COMMENT ON COLUMN users.roles IS 'Liste des roles globaux de securite du compte.'");
        $this->addSql("COMMENT ON COLUMN users.password IS 'Hash du mot de passe utilisateur.'");
        $this->addSql("COMMENT ON COLUMN users.employee_id IS 'Lien optionnel vers la fiche employe associee.'");

        // work_hours
        $this->addSql("COMMENT ON TABLE work_hours IS 'Saisie des heures de travail et presences par employe et par jour.'");
        $this->addSql("COMMENT ON COLUMN work_hours.id IS 'Identifiant technique de la ligne de saisie.'");
        $this->addSql("COMMENT ON COLUMN work_hours.employee_id IS 'Employe concerne par la saisie horaire.'");
        $this->addSql("COMMENT ON COLUMN work_hours.work_date IS 'Date de travail de la ligne de saisie.'");
        $this->addSql("COMMENT ON COLUMN work_hours.morning_from IS 'Heure de debut du matin.'");
        $this->addSql("COMMENT ON COLUMN work_hours.morning_to IS 'Heure de fin du matin.'");
        $this->addSql("COMMENT ON COLUMN work_hours.afternoon_from IS 'Heure de debut de l apres midi.'");
        $this->addSql("COMMENT ON COLUMN work_hours.afternoon_to IS 'Heure de fin de l apres midi.'");
        $this->addSql("COMMENT ON COLUMN work_hours.evening_from IS 'Heure de debut du soir.'");
        $this->addSql("COMMENT ON COLUMN work_hours.evening_to IS 'Heure de fin du soir.'");
        $this->addSql("COMMENT ON COLUMN work_hours.is_valid IS 'Validation RH de la ligne horaire.'");
        $this->addSql("COMMENT ON COLUMN work_hours.is_present_morning IS 'Presence declarative du matin (mode PRESENCE).'");
        $this->addSql("COMMENT ON COLUMN work_hours.is_present_afternoon IS 'Presence declarative de l apres midi (mode PRESENCE).'");
        $this->addSql("COMMENT ON COLUMN work_hours.is_site_valid IS 'Validation site (chef de site) de la ligne horaire.'");
    }

    public function down(Schema $schema): void
    {
        $tables = [
            'absence_types',
            'absences',
            'contracts',
            'doctrine_migration_versions',
            'employee_contract_periods',
            'employee_leave_balances',
            'employees',
            'notifications',
            'sites',
            'user_site_roles',
            'users',
            'work_hours',
        ];

        foreach ($tables as $table) {
            $this->addSql(sprintf('COMMENT ON TABLE %s IS NULL', $table));
        }

        $columns = [
            ['absence_types', 'id'],
            ['absence_types', 'code'],
            ['absence_types', 'label'],
            ['absence_types', 'color'],
            ['absence_types', 'count_as_worked_hours'],
            ['absences', 'id'],
            ['absences', 'employee_id'],
            ['absences', 'type_id'],
            ['absences', 'comment'],
            ['absences', 'start_date'],
            ['absences', 'end_date'],
            ['absences', 'start_half'],
            ['absences', 'end_half'],
            ['contracts', 'id'],
            ['contracts', 'name'],
            ['contracts', 'tracking_mode'],
            ['contracts', 'weekly_hours'],
            ['contracts', 'is_active'],
            ['doctrine_migration_versions', 'version'],
            ['doctrine_migration_versions', 'executed_at'],
            ['doctrine_migration_versions', 'execution_time'],
            ['employee_contract_periods', 'id'],
            ['employee_contract_periods', 'employee_id'],
            ['employee_contract_periods', 'contract_id'],
            ['employee_contract_periods', 'start_date'],
            ['employee_contract_periods', 'end_date'],
            ['employee_contract_periods', 'created_at'],
            ['employee_contract_periods', 'contract_nature'],
            ['employee_contract_periods', 'paid_leave_settled'],
            ['employee_leave_balances', 'id'],
            ['employee_leave_balances', 'employee_id'],
            ['employee_leave_balances', 'rule_code'],
            ['employee_leave_balances', 'year'],
            ['employee_leave_balances', 'opening_days'],
            ['employee_leave_balances', 'opening_saturdays'],
            ['employee_leave_balances', 'accrued_days'],
            ['employee_leave_balances', 'accrued_saturdays'],
            ['employee_leave_balances', 'taken_days'],
            ['employee_leave_balances', 'taken_saturdays'],
            ['employee_leave_balances', 'closing_days'],
            ['employee_leave_balances', 'closing_saturdays'],
            ['employee_leave_balances', 'is_locked'],
            ['employee_leave_balances', 'created_at'],
            ['employee_leave_balances', 'updated_at'],
            ['employees', 'id'],
            ['employees', 'first_name'],
            ['employees', 'last_name'],
            ['employees', 'created_at'],
            ['employees', 'site_id'],
            ['employees', 'display_order'],
            ['employees', 'contract_id'],
            ['notifications', 'id'],
            ['notifications', 'recipient_id'],
            ['notifications', 'title'],
            ['notifications', 'message'],
            ['notifications', 'is_read'],
            ['notifications', 'created_at'],
            ['sites', 'id'],
            ['sites', 'name'],
            ['sites', 'color'],
            ['sites', 'display_order'],
            ['user_site_roles', 'id'],
            ['user_site_roles', 'user_id'],
            ['user_site_roles', 'site_id'],
            ['user_site_roles', 'role'],
            ['users', 'id'],
            ['users', 'username'],
            ['users', 'roles'],
            ['users', 'password'],
            ['users', 'employee_id'],
            ['work_hours', 'id'],
            ['work_hours', 'employee_id'],
            ['work_hours', 'work_date'],
            ['work_hours', 'morning_from'],
            ['work_hours', 'morning_to'],
            ['work_hours', 'afternoon_from'],
            ['work_hours', 'afternoon_to'],
            ['work_hours', 'evening_from'],
            ['work_hours', 'evening_to'],
            ['work_hours', 'is_valid'],
            ['work_hours', 'is_present_morning'],
            ['work_hours', 'is_present_afternoon'],
            ['work_hours', 'is_site_valid'],
        ];

        foreach ($columns as [$table, $column]) {
            $this->addSql(sprintf('COMMENT ON COLUMN %s.%s IS NULL', $table, $column));
        }
    }
}
