<?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
        ]);
    }

    public function getByName($name)
    {
        $candidates = [$name, $name . ' ', rtrim($name)];
        $setting = TaxSettingMain::whereIn('name', $candidates)->first();

        if (!$setting) {
            return response()->json([
                'success' => false,
                'message' => 'Setting not found'
            ], 404);
        }

        $key = trim($setting->name);
        $data = [];
        if ($key === 'tax_status') {
            $data = ['tax_status' => (bool) ($setting->tax_status ?? false)];
        } elseif ($key === 'country') {
            $data = ['tax_country' => $setting->tax_country ?? null];
        } elseif ($key === 'province') {
            $data = ['tax_province' => $setting->tax_province ?? null];
        } elseif ($key === 'city') {
            $data = ['tax_city' => $setting->tax_city ?? null];
        } elseif ($key === 'vat_registered') {
            $data = ['vat_registered' => (bool) ($setting->vat_registered ?? false)];
        } elseif ($key === 'tax_scheme') {
            $data = ['tax_scheme' => $setting->tax_scheme ?? 'normal'];
        } elseif ($key === 'construction_certificate') {
            $data = ['construction_certificate' => (int) ($setting->construction_certificate ?? 0)];
        } elseif ($key === 'has_npwp') {
            $data = ['has_npwp' => (bool) ($setting->has_npwp ?? false)];
        } elseif ($key === 'import_foreign_product') {
            $data = ['import_foreign_product' => (int) ($setting->import_foreign_product ?? 0)];
        } else {
            $data = [$key => $setting];
        }

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