<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Business;
use App\Models\Subscription;
use App\Support\ApiResponse;
use Illuminate\Http\Request;

class BusinessController extends Controller
{
    public function index(Request $request)
    {
        $user = $request->user();
        $businesses = Business::where('user_id', $user->id)
            ->with('subscription')
            ->orderByDesc('id')
            ->get();
        return ApiResponse::success('Business list', $businesses);
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'businessName' => 'required|string|max:255',
            'businessShortName' => 'nullable|string|max:255',
            'documentInbox' => 'nullable|string|max:255',
            'businessType' => 'nullable|string|max:255',
            'organizationType' => 'nullable|string|max:255',
            'contactNumber' => 'nullable|string|max:50',
            'countryId' => 'nullable|integer',
            'currencyId' => 'nullable|integer',
            'stateId' => 'nullable|integer',
            'isOrganization' => 'nullable|boolean',
        ]);

        $business = Business::create([
            'user_id' => $request->user()->id,
            'business_name' => $data['businessName'],
            'business_short_name' => $data['businessShortName'] ?? null,
            'document_inbox' => $data['documentInbox'] ?? null,
            'business_type' => $data['businessType'] ?? null,
            'organization_type' => $data['organizationType'] ?? null,
            'contact_number' => $data['contactNumber'] ?? null,
            'country_id' => $data['countryId'] ?? null,
            'currency_id' => $data['currencyId'] ?? null,
            'state_id' => $data['stateId'] ?? null,
            'is_organization' => (bool) ($data['isOrganization'] ?? false),
        ]);

        Subscription::create([
            'business_id' => $business->id,
            'plan' => 'basic',
            'status' => 'active',
            'document_limit' => 100,
            'documents_used' => 0,
            'start_date' => now()->toDateString(),
            'end_date' => now()->addMonth()->toDateString(),
            'auto_renew' => true,
        ]);

        return ApiResponse::success('Business created', $business);
    }

    public function show(Request $request, int $id)
    {
        $business = Business::where('user_id', $request->user()->id)->findOrFail($id);
        return ApiResponse::success('Business', $business);
    }

    public function update(Request $request, int $id)
    {
        $business = Business::where('user_id', $request->user()->id)->findOrFail($id);
        $business->update($request->only([
            'business_name', 'business_short_name', 'document_inbox', 'business_type',
            'organization_type', 'contact_number', 'registration_number', 'address',
            'email', 'country_id', 'currency_id', 'state_id', 'is_organization',
        ]));
        return ApiResponse::success('Business updated', $business);
    }
}
