<?php
include_once __dir__ .  '/../core/Filter.php';
include_once __dir__ .  '/../core/LogEvent.php';

class Plans extends Core
{
    use Filter;
    
    function listOptions()
    {
        $companyId = (int) $_POST['companyId'];
        $sql = "
            (
                SELECT 
                    po.name,
                    COALESCE(sq.value, po.quota_value) AS value
                FROM company_signatures cs
                JOIN plan_payment_contracts ppc 
                    ON cs.plan_payment_id = ppc.id
                JOIN plan_options po 
                    ON po.plan_id = cs.plan_id
                   AND po.quota_period = ppc.period
                LEFT JOIN signature_quotes sq 
                    ON sq.plan_option_id = po.id 
                   AND sq.company_id = cs.company_id
                WHERE cs.company_id = :companyId AND cs.type = 'signature'
            )
            UNION ALL
            (
                SELECT 
                    'use_credit' AS name,
                    COALESCE(pc.use_credit, 0) AS value
                FROM plans_credits pc
                WHERE pc.company_id = :companyId
            )
        ";
        
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute(['companyId' => $companyId]);
        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // Monta objeto chave => valor
        $options = [];
        foreach ($rows as $row) {
            $options[$row['name']] = $row['value'];
        }
        
        // Resposta
        return ['type' => 'OK', 'data' => $options];  
    }
     
    function listDetailsPayment(){
        
        $companyId = (int) $_GET['companyId'];
        $sql = "SELECT * FROM `company_signatures` WHERE company_id = :companyId";
        
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute(['companyId' => $companyId]);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

        return ['type' => 'OK', 'data' => $result];
        
    }
    
    function listPlans(){
       
        $signId = $_GET['id'];
        $white_label_id = $this->config['WHITE_LABEL_ID'];
        $sql = "SELECT DISTINCT
                pl.name,
                cs.show_dialog,
               
                CASE
                    WHEN ppc.period IS NULL THEN 'mensal'
                    WHEN ppc.period = 'monthly' THEN 'mensal'
                    WHEN ppc.period = 'biannual' THEN 'semestral'
                    WHEN ppc.period = 'annual' THEN 'anual'
                    ELSE ppc.period
                END AS period,
           
                CASE
                    WHEN cs.payment_type IS NULL THEN 'Boleto'
                    WHEN cs.payment_type = 'BILLET' THEN 'Boleto'
                    WHEN cs.payment_type = 'SINGLE_BILLET' THEN 'Boleto'
                    WHEN cs.payment_type = 'CREDIT_CARD' THEN 'Cartão de Crédito'
                    ELSE cs.payment_type
                END AS payment_type,
           
                COALESCE(ppo.price, '0.00') AS total_price,
           
                CASE
                    WHEN ppo.installment IS NULL THEN 'à vista'
                    WHEN ppo.installment = '0' THEN 'à vista'
                    WHEN ppo.installment = '1' THEN 'parcelado'
                    ELSE ppo.installment
                END AS installment
           
            FROM company_signatures cs
            JOIN plans pl
                ON pl.id = cs.plan_id
           
            LEFT JOIN plan_payment_contracts ppc
                ON cs.plan_payment_id = ppc.id
           
            LEFT JOIN plan_options po
                ON po.plan_id = cs.plan_id
               AND (ppc.period IS NULL OR po.quota_period = ppc.period)
           
            LEFT JOIN plan_payment_options ppo
                ON ppo.plan_payment_id = ppc.id
               AND (ppo.id = cs.plan_payment_option_id OR ppo.price IS NULL)
           
            WHERE cs.signature_id = :signature_id
              AND cs.type = 'signature'
              AND pl.white_label_id = :white_label_id
        ";
       
        $stmt = parent::$conn->prepare($sql);
        $stmt->execute(['signature_id' => $signId, 'white_label_id' => $white_label_id]);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
 
        return ['type' => 'OK', 'data' => $result];
    }

    function updateShow(){
        $id = $_POST['companyId'];
        $updateSql = "UPDATE company_signatures SET show_dialog = 'true' WHERE company_id = :id";
        $updateStmt = parent::$conn->prepare($updateSql); 
        $updateStmt->execute([':id' => $id]);
        return ['type' => 'OK'];
    }
    
}