<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use DateInterval;
use DatePeriod;
use DateTimeImmutable;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration;

final class Version20260219180000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Decoupe les absences multi-jours en lignes journalieres.';
    }

    public function up(Schema $schema): void
    {
        $rows = $this->connection->fetchAllAssociative(
            'SELECT id, employee_id, type_id, start_date, end_date, start_half, end_half, comment
             FROM absences
             WHERE start_date < end_date
             ORDER BY id ASC'
        );

        foreach ($rows as $row) {
            $start = DateTimeImmutable::createFromFormat('Y-m-d', (string) $row['start_date']);
            $end   = DateTimeImmutable::createFromFormat('Y-m-d', (string) $row['end_date']);
            if (!$start instanceof DateTimeImmutable || !$end instanceof DateTimeImmutable) {
                continue;
            }

            $startHalf = (string) $row['start_half'];
            $endHalf   = (string) $row['end_half'];

            $days = new DatePeriod($start, new DateInterval('P1D'), $end->modify('+1 day'));
            foreach ($days as $day) {
                $isFirst = $day->format('Y-m-d') === $start->format('Y-m-d');
                $isLast  = $day->format('Y-m-d') === $end->format('Y-m-d');

                if ($isFirst && 'PM' === $startHalf) {
                    $segmentStartHalf = 'PM';
                    $segmentEndHalf   = 'PM';
                } elseif ($isLast && 'AM' === $endHalf) {
                    $segmentStartHalf = 'AM';
                    $segmentEndHalf   = 'AM';
                } else {
                    $segmentStartHalf = 'AM';
                    $segmentEndHalf   = 'PM';
                }

                $this->connection->insert('absences', [
                    'employee_id' => (int) $row['employee_id'],
                    'type_id'     => (int) $row['type_id'],
                    'start_date'  => $day,
                    'end_date'    => $day,
                    'start_half'  => $segmentStartHalf,
                    'end_half'    => $segmentEndHalf,
                    'comment'     => $row['comment'],
                ], [
                    'employee_id' => Types::INTEGER,
                    'type_id'     => Types::INTEGER,
                    'start_date'  => Types::DATE_IMMUTABLE,
                    'end_date'    => Types::DATE_IMMUTABLE,
                    'start_half'  => Types::STRING,
                    'end_half'    => Types::STRING,
                    'comment'     => Types::TEXT,
                ]);
            }

            $this->connection->delete('absences', ['id' => (int) $row['id']], ['id' => Types::INTEGER]);
        }
    }

    public function down(Schema $schema): void
    {
        $this->throwIrreversibleMigrationException('Cette migration de decoupage est irreversible.');
    }
}
