<?php

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

class CreateOwnerRewardsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::create('owner_rewards', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('owner_id')->comment('オーナーID');
            $table->unsignedBigInteger('order_id')->comment('対象注文ID');
            $table->unsignedBigInteger('product_id')->comment('対象商品ID');
            $table->integer('rental_amount')->comment('レンタル料金');
            $table->integer('reward_amount')->comment('報酬額（固定額）');
            $table->integer('platform_fee')->comment('KIREI売上（レンタル料金 − 報酬額）');
            $table->unsignedSmallInteger('payment_method')->default(0)->comment('0=PAY.JP自動, 1=手動振込');
            $table->unsignedSmallInteger('transfer_status')->default(0)->comment('0=未振込, 1=振込済み');
            $table->timestamp('transferred_at')->nullable()->comment('振込日');
            $table->string('payjp_transfer_id')->nullable()->comment('PAY.JP振込ID（自動の場合）');
            $table->timestamps();

            $table->foreign('owner_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->index(['owner_id', 'transfer_status']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::dropIfExists('owner_rewards');
    }
}
