<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\Uid\Uuid;

final class Version20260614181657 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Seed subscription plans and trial periods';
    }

    public function up(Schema $schema): void
    {
        $now = time();

        // Plans
        $freePlanUuid         = Uuid::v4()->toRfc4122();
        $basicPlanUuid        = Uuid::v4()->toRfc4122();
        $professionalPlanUuid = Uuid::v4()->toRfc4122();

        $this->addSql("INSERT INTO subscription_plans (uuid, name, level, max_secretaries, features, active, created_at, updated_at) VALUES
            ('{$freePlanUuid}',         'free',         0, 1, '{\"patient_records\":false,\"services\":false,\"sms_panel\":false}', 1, {$now}, {$now}),
            ('{$basicPlanUuid}',        'basic',        1, 3, '{\"patient_records\":true,\"services\":true,\"sms_panel\":false}',   1, {$now}, {$now}),
            ('{$professionalPlanUuid}', 'professional', 2, 10,'{\"patient_records\":true,\"services\":true,\"sms_panel\":true}',   1, {$now}, {$now})
        ");

        // Periods for basic plan: trial (7 days = 1/4 month), 1-month, 3-month, 12-month
        $this->addSql("
            INSERT INTO subscription_periods (uuid, plan_id, label, duration_months, price_rials, is_trial, active, sort_order, created_at, updated_at)
            SELECT '" . Uuid::v4()->toRfc4122() . "', id, 'تریال ۷ روزه', 1, 0, 1, 1, 0, {$now}, {$now} FROM subscription_plans WHERE name = 'basic'
        ");

        // Professional trial
        $this->addSql("
            INSERT INTO subscription_periods (uuid, plan_id, label, duration_months, price_rials, is_trial, active, sort_order, created_at, updated_at)
            SELECT '" . Uuid::v4()->toRfc4122() . "', id, 'تریال ۷ روزه', 1, 0, 1, 1, 0, {$now}, {$now} FROM subscription_plans WHERE name = 'professional'
        ");

        // Basic paid periods
        $this->addSql("
            INSERT INTO subscription_periods (uuid, plan_id, label, duration_months, price_rials, is_trial, active, sort_order, created_at, updated_at)
            SELECT '" . Uuid::v4()->toRfc4122() . "', id, 'یک ماهه', 1, 290000, 0, 1, 1, {$now}, {$now} FROM subscription_plans WHERE name = 'basic'
        ");
        $this->addSql("
            INSERT INTO subscription_periods (uuid, plan_id, label, duration_months, price_rials, is_trial, active, sort_order, created_at, updated_at)
            SELECT '" . Uuid::v4()->toRfc4122() . "', id, 'سه ماهه', 3, 790000, 0, 1, 2, {$now}, {$now} FROM subscription_plans WHERE name = 'basic'
        ");
        $this->addSql("
            INSERT INTO subscription_periods (uuid, plan_id, label, duration_months, price_rials, is_trial, active, sort_order, created_at, updated_at)
            SELECT '" . Uuid::v4()->toRfc4122() . "', id, 'یک ساله', 12, 2900000, 0, 1, 3, {$now}, {$now} FROM subscription_plans WHERE name = 'basic'
        ");

        // Professional paid periods
        $this->addSql("
            INSERT INTO subscription_periods (uuid, plan_id, label, duration_months, price_rials, is_trial, active, sort_order, created_at, updated_at)
            SELECT '" . Uuid::v4()->toRfc4122() . "', id, 'یک ماهه', 1, 590000, 0, 1, 1, {$now}, {$now} FROM subscription_plans WHERE name = 'professional'
        ");
        $this->addSql("
            INSERT INTO subscription_periods (uuid, plan_id, label, duration_months, price_rials, is_trial, active, sort_order, created_at, updated_at)
            SELECT '" . Uuid::v4()->toRfc4122() . "', id, 'سه ماهه', 3, 1590000, 0, 1, 2, {$now}, {$now} FROM subscription_plans WHERE name = 'professional'
        ");
        $this->addSql("
            INSERT INTO subscription_periods (uuid, plan_id, label, duration_months, price_rials, is_trial, active, sort_order, created_at, updated_at)
            SELECT '" . Uuid::v4()->toRfc4122() . "', id, 'یک ساله', 12, 5900000, 0, 1, 3, {$now}, {$now} FROM subscription_plans WHERE name = 'professional'
        ");

        // SiteConfig: trial_enabled
        $this->addSql("INSERT IGNORE INTO site_config (config_key, config_value) VALUES ('trial_enabled', '1')");
        $this->addSql("INSERT IGNORE INTO site_config (config_key, config_value) VALUES ('sms_price_rials', '500')");
    }

    public function down(Schema $schema): void
    {
        $this->addSql("DELETE FROM subscription_periods");
        $this->addSql("DELETE FROM subscription_plans");
        $this->addSql("DELETE FROM site_config WHERE config_key IN ('trial_enabled', 'sms_price_rials')");
    }
}
