<?php

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

/**
 * 바로빌 월정액 구독 테이블
 *
 * 회원사별 월정액 서비스 구독 현황 관리
 * - 계좌조회, 카드내역, 홈텍스 매입/매출
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('barobill_subscriptions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('member_id')->comment('바로빌 회원사 ID');
            $table->enum('service_type', ['bank_account', 'card', 'hometax'])
                  ->comment('서비스 유형: bank_account=계좌조회, card=카드내역, hometax=홈텍스');
            $table->unsignedInteger('monthly_fee')->default(0)->comment('월정액 금액 (원)');
            $table->date('started_at')->comment('구독 시작일');
            $table->date('ended_at')->nullable()->comment('구독 종료일 (null=진행중)');
            $table->boolean('is_active')->default(true)->comment('활성 상태');
            $table->text('memo')->nullable()->comment('메모');
            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index(['member_id', 'service_type']);
            $table->index(['is_active', 'service_type']);

            // 외래키
            $table->foreign('member_id')
                  ->references('id')
                  ->on('barobill_members')
                  ->onDelete('cascade');
        });
    }

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