<?php

namespace App\Services\TenantBootstrap\Steps;

use App\Services\TenantBootstrap\Contracts\TenantBootstrapStep;
use Illuminate\Support\Facades\DB;

class ApprovalFormsStep implements TenantBootstrapStep
{
    public function key(): string
    {
        return 'approval_forms_seed';
    }

    public function run(int $tenantId): void
    {
        if (! DB::getSchemaBuilder()->hasTable('approval_forms')) {
            return;
        }

        $now = now();
        $forms = [
            [
                'name' => '품의서',
                'code' => 'proposal',
                'category' => '일반',
                'template' => json_encode([
                    'fields' => [
                        ['name' => 'title', 'type' => 'text', 'label' => '제목', 'required' => true],
                        ['name' => 'vendor', 'type' => 'text', 'label' => '거래처', 'required' => false],
                        ['name' => 'description', 'type' => 'textarea', 'label' => '내용', 'required' => true],
                        ['name' => 'reason', 'type' => 'textarea', 'label' => '사유', 'required' => true],
                        ['name' => 'estimatedCost', 'type' => 'number', 'label' => '예상비용', 'required' => false],
                    ],
                ]),
            ],
            [
                'name' => '지출결의서',
                'code' => 'expenseReport',
                'category' => '경비',
                'template' => json_encode([
                    'fields' => [
                        ['name' => 'requestDate', 'type' => 'date', 'label' => '신청일', 'required' => true],
                        ['name' => 'paymentDate', 'type' => 'date', 'label' => '지급일', 'required' => true],
                        ['name' => 'items', 'type' => 'array', 'label' => '지출항목', 'required' => true],
                        ['name' => 'totalAmount', 'type' => 'number', 'label' => '총액', 'required' => true],
                    ],
                ]),
            ],
            [
                'name' => '비용견적서',
                'code' => 'expenseEstimate',
                'category' => '경비',
                'template' => json_encode([
                    'fields' => [
                        ['name' => 'items', 'type' => 'array', 'label' => '비용항목', 'required' => true],
                        ['name' => 'totalExpense', 'type' => 'number', 'label' => '총지출', 'required' => true],
                        ['name' => 'accountBalance', 'type' => 'number', 'label' => '계좌잔액', 'required' => true],
                    ],
                ]),
            ],
            [
                'name' => '근태신청',
                'code' => 'attendance_request',
                'category' => '일반',
                'template' => json_encode([
                    'fields' => [
                        ['name' => 'user_name', 'type' => 'text', 'label' => '신청자', 'required' => true],
                        ['name' => 'request_type', 'type' => 'select', 'label' => '신청유형', 'required' => true],
                        ['name' => 'period', 'type' => 'daterange', 'label' => '기간', 'required' => true],
                        ['name' => 'days', 'type' => 'number', 'label' => '일수', 'required' => true],
                        ['name' => 'reason', 'type' => 'textarea', 'label' => '사유', 'required' => true],
                    ],
                ]),
            ],
            [
                'name' => '사유서',
                'code' => 'reason_report',
                'category' => '일반',
                'template' => json_encode([
                    'fields' => [
                        ['name' => 'user_name', 'type' => 'text', 'label' => '작성자', 'required' => true],
                        ['name' => 'report_type', 'type' => 'select', 'label' => '사유유형', 'required' => true],
                        ['name' => 'target_date', 'type' => 'date', 'label' => '대상일', 'required' => true],
                        ['name' => 'reason', 'type' => 'textarea', 'label' => '사유', 'required' => true],
                    ],
                ]),
            ],
        ];

        foreach ($forms as $form) {
            DB::table('approval_forms')->updateOrInsert(
                ['tenant_id' => $tenantId, 'code' => $form['code']],
                [
                    'name' => $form['name'],
                    'category' => $form['category'],
                    'template' => $form['template'],
                    'is_active' => true,
                    'updated_at' => $now,
                    'created_at' => $now,
                ]
            );
        }
    }
}
