<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * 사업자등록증 OCR 데이터 모델
 */
class BizCert extends Model
{
    protected $table = 'biz_cert';

    protected $fillable = [
        'biz_no',
        'company_name',
        'representative',
        'open_date',
        'address',
        'biz_type',
        'biz_item',
        'issue_date',
        'raw_text',
        'ocr_method',
    ];

    protected function casts(): array
    {
        return [
            'open_date' => 'date',
            'issue_date' => 'date',
        ];
    }

    /**
     * 사업자번호 포맷팅 (XXX-XX-XXXXX)
     */
    public function getFormattedBizNoAttribute(): string
    {
        $no = preg_replace('/[^0-9]/', '', $this->biz_no);
        if (strlen($no) === 10) {
            return substr($no, 0, 3).'-'.substr($no, 3, 2).'-'.substr($no, 5, 5);
        }

        return $this->biz_no;
    }
}
