<?php
/**
 * Payment model
 *
 * @package NemtlyBooking
 */

namespace NemtlyBooking\Models;

/**
 * Class Payment
 */
class Payment {
    /**
     * Payment ID
     *
     * @var int
     */
    private $id;

    /**
     * Payable ID
     *
     * @var int
     */
    private $payable_id;

    /**
     * Payable Type
     *
     * @var string
     */
    private $payable_type;

    /**
     * Booking ID
     *
     * @var int
     */
    private $booking_id;

    /**
     * Transaction ID
     *
     * @var string
     */
    private $transaction_id;

    /**
     * Amount
     *
     * @var float
     */
    private $amount;

    /**
     * Currency
     *
     * @var string
     */
    private $currency;

    /**
     * Status
     *
     * @var string
     */
    private $status;

    /**
     * Gateway
     *
     * @var string
     */
    private $gateway;

    /**
     * Gateway response
     *
     * @var string
     */
    private $gateway_response;

    /**
     * Created at
     *
     * @var string
     */
    private $created_at;

    /**
     * Updated at
     *
     * @var string
     */
    private $updated_at;

    /**
     * Constructor
     *
     * @param array $args Payment data.
     */
    public function __construct( $args = [] ) {
        $this->id           = ! empty( $args['id'] ) ? (int) $args['id'] : 0;
        $this->payable_id   = ! empty( $args['payable_id'] ) ? (int) $args['payable_id'] : 0;
        $this->payable_type = ! empty( $args['payable_type'] ) ? $args['payable_type'] : 'booking';

        // Backward compatibility: If booking_id is set but payable_id is not
        if ( empty( $this->payable_id ) && ! empty( $args['booking_id'] ) ) {
            $this->payable_id   = (int) $args['booking_id'];
            $this->payable_type = 'booking';
        }

        $this->booking_id       = ! empty( $args['booking_id'] ) ? (int) $args['booking_id'] : $this->payable_id;
        $this->transaction_id   = ! empty( $args['transaction_id'] ) ? $args['transaction_id'] : '';
        $this->amount           = ! empty( $args['amount'] ) ? (float) $args['amount'] : 0.0;
        $this->currency         = ! empty( $args['currency'] ) ? $args['currency'] : 'USD';
        $this->status           = ! empty( $args['status'] ) ? $args['status'] : 'pending';
        $this->gateway          = ! empty( $args['gateway'] ) ? $args['gateway'] : 'stripe';
        $this->gateway_response = ! empty( $args['gateway_response'] ) ? $args['gateway_response'] : '';
        $this->created_at       = ! empty( $args['created_at'] ) ? $args['created_at'] : current_time( 'mysql' );
        $this->updated_at       = ! empty( $args['updated_at'] ) ? $args['updated_at'] : current_time( 'mysql' );
    }

    /**
     * Get ID
     *
     * @return int
     */
    public function get_id() {
        return $this->id;
    }

    /**
     * Get payable ID
     *
     * @return int
     */
    public function get_payable_id() {
        return $this->payable_id;
    }

    /**
     * Get payable type
     *
     * @return string
     */
    public function get_payable_type() {
        return $this->payable_type;
    }

    /**
     * Get booking ID
     *
     * @return int
     */
    public function get_booking_id() {
        return $this->booking_id;
    }

    /**
     * Get transaction ID
     *
     * @return string
     */
    public function get_transaction_id() {
        return $this->transaction_id;
    }

    /**
     * Get amount
     *
     * @return float
     */
    public function get_amount() {
        return $this->amount;
    }

    /**
     * Get currency
     *
     * @return string
     */
    public function get_currency() {
        return $this->currency;
    }

    /**
     * Get status
     *
     * @return string
     */
    public function get_status() {
        return $this->status;
    }

    /**
     * Get gateway
     *
     * @return string
     */
    public function get_gateway() {
        return $this->gateway;
    }

    /**
     * Get gateway response
     *
     * @return string
     */
    public function get_gateway_response() {
        return $this->gateway_response;
    }

    /**
     * Get created at
     *
     * @return string
     */
    public function get_created_at() {
        return $this->created_at;
    }

    /**
     * Get updated at
     *
     * @return string
     */
    public function get_updated_at() {
        return $this->updated_at;
    }

    /**
     * Convert to array
     *
     * @return array
     */
    public function to_array() {
        return [
            'id'               => $this->id,
            'payable_id'       => $this->payable_id,
            'payable_type'     => $this->payable_type,
            'booking_id'       => $this->booking_id,
            'transaction_id'   => $this->transaction_id,
            'amount'           => $this->amount,
            'currency'         => $this->currency,
            'status'           => $this->status,
            'gateway'          => $this->gateway,
            'gateway_response' => $this->gateway_response,
            'created_at'       => $this->created_at,
            'updated_at'       => $this->updated_at,
        ];
    }
}
