<?php

namespace App\Http\Requests\Client;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ClientStoreRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    protected function prepareForValidation(): void
    {
        $this->convertCommonCodeNameToCode('client_type');
        $this->convertCommonCodeNameToCode('bad_debt_progress');
    }

    /**
     * common_codes의 name을 code로 변환
     */
    private function convertCommonCodeNameToCode(string $field): void
    {
        $value = $this->input($field);
        if (! $value) {
            return;
        }

        // 이미 code인지 확인
        $existsAsCode = \DB::table('common_codes')
            ->where('code_group', $field)
            ->where('code', $value)
            ->exists();

        if ($existsAsCode) {
            return;
        }

        // name으로 code 조회
        $code = \DB::table('common_codes')
            ->where('code_group', $field)
            ->where('name', $value)
            ->value('code');

        if ($code) {
            $this->merge([$field => $code]);
        }
    }

    public function rules(): array
    {
        return [
            'client_group_id' => 'nullable|integer',
            'client_code' => 'nullable|string|max:50', // 프론트에서 보내도 백엔드에서 자동 생성
            'name' => 'required|string|max:100',
            'client_type' => [
                'nullable',
                Rule::exists('common_codes', 'code')->where('code_group', 'client_type'),
            ],
            // 연락처 정보
            'contact_person' => 'nullable|string|max:100',
            'phone' => 'nullable|string|max:20',
            'mobile' => 'nullable|string|max:20',
            'fax' => 'nullable|string|max:20',
            'email' => 'nullable|email|max:100',
            'address' => 'nullable|string|max:500',
            // 담당자 정보
            'manager_name' => 'nullable|string|max:50',
            'manager_tel' => 'nullable|string|max:20',
            'system_manager' => 'nullable|string|max:50',
            // 발주처 설정
            'account_id' => 'nullable|string|max:50',
            'account_password' => 'nullable|string|max:255',
            'purchase_payment_day' => 'nullable|string|max:20',
            'sales_payment_day' => 'nullable|string|max:20',
            // 사업자 정보
            'business_no' => 'nullable|string|max:20',
            'business_type' => 'nullable|string|max:50',
            'business_item' => 'nullable|string|max:100',
            // 약정 세금
            'tax_agreement' => 'nullable|boolean',
            'tax_amount' => 'nullable|numeric|min:0',
            'tax_start_date' => 'nullable|date',
            'tax_end_date' => 'nullable|date|after_or_equal:tax_start_date',
            // 악성채권 정보
            'bad_debt' => 'nullable|boolean',
            'bad_debt_amount' => 'nullable|numeric|min:0',
            'bad_debt_receive_date' => 'nullable|date',
            'bad_debt_end_date' => 'nullable|date|after_or_equal:bad_debt_receive_date',
            'bad_debt_progress' => [
                'nullable',
                Rule::exists('common_codes', 'code')->where('code_group', 'bad_debt_progress'),
            ],
            // 기타
            'memo' => 'nullable|string',
            'is_active' => 'nullable|boolean',
            'is_overdue' => 'nullable|boolean',
        ];
    }
}
