<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * INT-02 from the system analysis: "payment method" was purely a label
 * (Cash/Card/Account) with zero actual processing behind it. This adds
 * real Stripe PaymentIntent support, per-company (each tenant uses their
 * own Stripe account, same pattern as the existing per-company Twilio
 * credentials) so no card data ever touches this server directly — Stripe's
 * client-side SDK collects it, we only ever see a token/intent id.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('companies', function (Blueprint $table) {
            $table->string('stripe_publishable_key')->nullable()->after('whatsapp_sender_id');
            $table->text('stripe_secret_key')->nullable()->after('stripe_publishable_key');
            $table->string('stripe_webhook_secret')->nullable()->after('stripe_secret_key');
        });

        Schema::create('payments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('booking_id')->constrained()->cascadeOnDelete();
            $table->string('provider')->default('stripe');
            $table->string('provider_intent_id')->nullable()->index();
            $table->decimal('amount', 10, 2);
            $table->string('currency', 3);
            $table->enum('status', ['pending', 'succeeded', 'failed'])->default('pending');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('payments');

        Schema::table('companies', function (Blueprint $table) {
            $table->dropColumn(['stripe_publishable_key', 'stripe_secret_key', 'stripe_webhook_secret']);
        });
    }
};
