<?php

namespace App\Http\Controllers;

use App\Models\TaxSettingMain;
use Illuminate\Http\Request;

class TaxSettingController extends Controller
{
    public function update(Request $request)
    {
        $request->validate([
            'tax_status' => 'required|boolean',
            'tax_country' => 'nullable|string',
            'tax_province' => 'nullable|string',
            'tax_city' => 'nullable|string',
            'vat_registered' => 'required|boolean',
            'tax_scheme' => 'nullable|string',
            'construction_certificate' => 'nullable|integer',
            'has_npwp' => 'nullable|boolean',
            'import_foreign_product' => 'nullable|integer',
        ]);

        // Update tax_status
        TaxSettingMain::updateOrCreate(
            ['name' => 'tax_status'],
            ['tax_status' => $request->tax_status]
        );

        // Update country
        TaxSettingMain::updateOrCreate(
            ['name' => 'country'],
            ['tax_country' => $request->tax_country]
        );

        // Update province
        TaxSettingMain::updateOrCreate(
            ['name' => 'province'],
            ['tax_province' => $request->tax_province]
        );

        // Update city
        TaxSettingMain::updateOrCreate(
            ['name' => 'city'],
            ['tax_city' => $request->tax_city]
        );

        // Update vat_registered (note the space in name from Seeder)
        TaxSettingMain::updateOrCreate(
            ['name' => 'vat_registered '],
            ['vat_registered' => $request->vat_registered]
        );

        // Update tax_scheme
        TaxSettingMain::updateOrCreate(
            ['name' => 'tax_scheme'],
            ['tax_scheme' => $request->tax_scheme]
        );

        // Update construction_certificate
        TaxSettingMain::updateOrCreate(
            ['name' => 'construction_certificate'],
            ['construction_certificate' => $request->construction_certificate]
        );

        // Update has_npwp
        TaxSettingMain::updateOrCreate(
            ['name' => 'has_npwp'],
            ['has_npwp' => $request->has_npwp]
        );

        // Update import_foreign_product
        TaxSettingMain::updateOrCreate(
            ['name' => 'import_foreign_product'],
            ['import_foreign_product' => $request->import_foreign_product]
        );

        return response()->json([
            'success' => true,
            'message' => 'Tax settings updated successfully',
        ]);
    }

    public function show()
    {
        $settings = TaxSettingMain::all();

        $data = [
            'tax_status' => $settings->where('name', 'tax_status')->first()->tax_status ?? false,
            'tax_country' => $settings->where('name', 'country')->first()->tax_country ?? null,
            'tax_province' => $settings->where('name', 'province')->first()->tax_province ?? null,
            'tax_city' => $settings->where('name', 'city')->first()->tax_city ?? null,
            'vat_registered' => $settings->where('name', 'vat_registered ')->first()->vat_registered ?? false,
            'tax_scheme' => $settings->where('name', 'tax_scheme')->first()->tax_scheme ?? 'normal',
            'construction_certificate' => $settings->where('name', 'construction_certificate')->first()->construction_certificate ?? 0,
            'has_npwp' => $settings->where('name', 'has_npwp')->first()->has_npwp ?? false,
            'import_foreign_product' => $settings->where('name', 'import_foreign_product')->first()->import_foreign_product ?? 0,
        ];

        return response()->json([
            'success' => true,
            'data' => $data
        ]);
    }
}
