<?php

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

class CreateShippingSystemTables extends Migration
{
    public function up()
    {
        // 都道府県マスタ
        Schema::create('prefectures', function (Blueprint $table) {
            $table->unsignedTinyInteger('id')->primary();
            $table->string('name', 10);
            $table->string('region', 10)->comment('地方名');
        });

        // 送料テーブル（金額帯ごとの送料設定）
        Schema::create('shipping_rates', function (Blueprint $table) {
            $table->id();
            $table->integer('price_from')->comment('商品価格（税抜）の下限');
            $table->integer('normal_cost')->default(0)->comment('通常地域の送料');
            $table->integer('special_cost')->default(0)->comment('特別地域の送料');
            $table->integer('position')->default(0);
            $table->timestamps();
        });

        // 特別送料都道府県
        Schema::create('shipping_special_prefectures', function (Blueprint $table) {
            $table->id();
            $table->unsignedTinyInteger('prefecture_id');
            $table->timestamps();
        });

        // 離島郵便番号マスタ
        Schema::create('shipping_remote_islands', function (Blueprint $table) {
            $table->id();
            $table->string('postal_code', 7)->index();
            $table->string('prefecture', 10)->nullable();
            $table->string('city', 100)->nullable();
            $table->string('area', 200)->nullable();
            $table->timestamps();
        });

        // 離島送料を適用するかのフラグ
        Schema::create('shipping_settings', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique();
            $table->string('value')->nullable();
            $table->timestamps();
        });

        // 都道府県データ投入
        $prefectures = [
            [1, '北海道', '北海道'], [2, '青森県', '東北'], [3, '岩手県', '東北'], [4, '宮城県', '東北'],
            [5, '秋田県', '東北'], [6, '山形県', '東北'], [7, '福島県', '東北'], [8, '茨城県', '関東'],
            [9, '栃木県', '関東'], [10, '群馬県', '関東'], [11, '埼玉県', '関東'], [12, '千葉県', '関東'],
            [13, '東京都', '関東'], [14, '神奈川県', '関東'], [15, '新潟県', '中部'], [16, '富山県', '中部'],
            [17, '石川県', '中部'], [18, '福井県', '中部'], [19, '山梨県', '中部'], [20, '長野県', '中部'],
            [21, '岐阜県', '中部'], [22, '静岡県', '中部'], [23, '愛知県', '中部'], [24, '三重県', '近畿'],
            [25, '滋賀県', '近畿'], [26, '京都府', '近畿'], [27, '大阪府', '近畿'], [28, '兵庫県', '近畿'],
            [29, '奈良県', '近畿'], [30, '和歌山県', '近畿'], [31, '鳥取県', '中国'], [32, '島根県', '中国'],
            [33, '岡山県', '中国'], [34, '広島県', '中国'], [35, '山口県', '中国'], [36, '徳島県', '四国'],
            [37, '香川県', '四国'], [38, '愛媛県', '四国'], [39, '高知県', '四国'], [40, '福岡県', '九州'],
            [41, '佐賀県', '九州'], [42, '長崎県', '九州'], [43, '熊本県', '九州'], [44, '大分県', '九州'],
            [45, '宮崎県', '九州'], [46, '鹿児島県', '九州'], [47, '沖縄県', '沖縄'],
        ];
        foreach ($prefectures as $p) {
            DB::table('prefectures')->insert(['id' => $p[0], 'name' => $p[1], 'region' => $p[2]]);
        }

        // 送料初期データ
        DB::table('shipping_rates')->insert([
            ['price_from' => 88000, 'normal_cost' => 0, 'special_cost' => 3000, 'position' => 1, 'created_at' => now(), 'updated_at' => now()],
            ['price_from' => 58000, 'normal_cost' => 0, 'special_cost' => 3000, 'position' => 2, 'created_at' => now(), 'updated_at' => now()],
            ['price_from' => 38000, 'normal_cost' => 0, 'special_cost' => 3000, 'position' => 3, 'created_at' => now(), 'updated_at' => now()],
            ['price_from' => 19800, 'normal_cost' => 0, 'special_cost' => 3000, 'position' => 4, 'created_at' => now(), 'updated_at' => now()],
            ['price_from' => 9800, 'normal_cost' => 3000, 'special_cost' => 6000, 'position' => 5, 'created_at' => now(), 'updated_at' => now()],
            ['price_from' => 0, 'normal_cost' => 3000, 'special_cost' => 6000, 'position' => 6, 'created_at' => now(), 'updated_at' => now()],
        ]);

        // 特別都道府県初期データ（北海道・沖縄）
        DB::table('shipping_special_prefectures')->insert([
            ['prefecture_id' => 1, 'created_at' => now(), 'updated_at' => now()],
            ['prefecture_id' => 47, 'created_at' => now(), 'updated_at' => now()],
        ]);

        // 離島設定フラグ
        DB::table('shipping_settings')->insert([
            ['key' => 'remote_island_enabled', 'value' => '1', 'created_at' => now(), 'updated_at' => now()],
        ]);

        // 離島郵便番号の初期データは管理画面のCSVインポートで投入してください
        // CSVテンプレートは管理画面（配送料設定 > 離島郵便番号管理）からダウンロードできます
        // 初期データCSV: database/seeders/data/shipping_remote_islands.csv
    }

    public function down()
    {
        Schema::dropIfExists('shipping_settings');
        Schema::dropIfExists('shipping_remote_islands');
        Schema::dropIfExists('shipping_special_prefectures');
        Schema::dropIfExists('shipping_rates');
        Schema::dropIfExists('prefectures');
    }
}
