<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Response;
use App\Helpers\GSTHelper;
use App\Models\TenantModel;

class GSTController extends Controller
{
    public function gstr1(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $month = (int) ($_GET['month'] ?? date('m'));
        $year = (int) ($_GET['year'] ?? date('Y'));

        try {
            $data = GSTHelper::getGSTR1Data($tenantId, $month, $year);
            Response::success($data);
        } catch (\Exception $e) {
            Response::error($e->getMessage());
        }
    }

    public function gstr3b(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $month = (int) ($_GET['month'] ?? date('m'));
        $year = (int) ($_GET['year'] ?? date('Y'));

        try {
            $data = GSTHelper::getGSTR3BData($tenantId, $month, $year);
            Response::success($data);
        } catch (\Exception $e) {
            Response::error($e->getMessage());
        }
    }

    public function hsnSummary(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $from = $_GET['from'] ?? date('Y-m-01');
        $to = $_GET['to'] ?? date('Y-m-t');

        $data = GSTHelper::getHSNSummary($tenantId, $from, $to);
        Response::success($data);
    }

    public function itcRegister(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $from = $_GET['from'] ?? date('Y-m-01');
        $to = $_GET['to'] ?? date('Y-m-t');

        $data = GSTHelper::getITCLedger($tenantId, $from, $to);
        Response::success($data);
    }

    public function liability(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $month = (int) ($_GET['month'] ?? date('m'));
        $year = (int) ($_GET['year'] ?? date('Y'));
        
        $data = GSTHelper::getGSTR3BData($tenantId, $month, $year);
        Response::success($data);
    }

    public function b2b(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $month = (int) ($_GET['month'] ?? date('m'));
        $year = (int) ($_GET['year'] ?? date('Y'));
        
        $data = GSTHelper::getGSTR1Data($tenantId, $month, $year);
        Response::success($data['b2b']);
    }

    public function cdnr(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $month = (int) ($_GET['month'] ?? date('m'));
        $year = (int) ($_GET['year'] ?? date('Y'));
        
        $data = GSTHelper::getGSTR1Data($tenantId, $month, $year);
        Response::success($data['cdnr']);
    }

    public function exportGstr1Json(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $month = (int) ($_GET['month'] ?? date('m'));
        $year = (int) ($_GET['year'] ?? date('Y'));

        $tenantModel = new TenantModel();
        $tenant = $tenantModel->find($tenantId);
        
        $json = GSTHelper::exportGSTR1JSON($tenantId, $month, $year, $tenant['gstin'] ?? '');

        header('Content-Type: application/json');
        header('Content-Disposition: attachment; filename="GSTR1_' . $month . '_' . $year . '.json"');
        echo $json;
        exit;
    }

    public function exportGstr3bJson(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $month = (int) ($_GET['month'] ?? date('m'));
        $year = (int) ($_GET['year'] ?? date('Y'));

        $tenantModel = new TenantModel();
        $tenant = $tenantModel->find($tenantId);
        
        $json = GSTHelper::exportGSTR3BJSON($tenantId, $month, $year, $tenant['gstin'] ?? '');

        header('Content-Type: application/json');
        header('Content-Disposition: attachment; filename="GSTR3B_' . $month . '_' . $year . '.json"');
        echo $json;
        exit;
    }
}
