<?php

namespace Laravel\Cashier;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Stripe\Discount as StripeDiscount;
use Stripe\PromotionCode as StripePromotionCode;

class Discount implements Arrayable, Jsonable, JsonSerializable
{
    /**
     * Create a new Discount instance.
     *
     * @param  \Stripe\Discount  $discount
     * @return void
     */
    public function __construct(protected StripeDiscount $discount)
    {
        //
    }

    /**
     * Get the coupon applied to the discount.
     *
     * @return \Laravel\Cashier\Coupon
     */
    public function coupon(): Coupon
    {
        return new Coupon($this->discount->coupon);
    }

    /**
     * Get the promotion code applied to create this discount.
     *
     * @return \Laravel\Cashier\PromotionCode|null
     */
    public function promotionCode(): ?PromotionCode
    {
        if (is_null($this->discount->promotion_code)) {
            return null;
        }

        // If promotion_code is already expanded as an object, use it...
        if (is_object($this->discount->promotion_code) && isset($this->discount->promotion_code->id)) {
            return new PromotionCode($this->discount->promotion_code);
        }

        // If promotion_code is just an ID string, fetch it from Stripe...
        if (is_string($this->discount->promotion_code)) {
            $promotionCode = StripePromotionCode::retrieve($this->discount->promotion_code);

            return new PromotionCode($promotionCode);
        }

        return null;
    }

    /**
     * Get the date that the coupon was applied.
     *
     * @return \Carbon\CarbonInterface
     */
    public function start(): CarbonInterface
    {
        return Carbon::createFromTimestamp($this->discount->start);
    }

    /**
     * Get the date that this discount will end.
     *
     * @return \Carbon\CarbonInterface|null
     */
    public function end(): ?CarbonInterface
    {
        if (! is_null($this->discount->end)) {
            return Carbon::createFromTimestamp($this->discount->end);
        }

        return null;
    }

    /**
     * Get the Stripe Discount instance.
     *
     * @return \Stripe\Discount
     */
    public function asStripeDiscount()
    {
        return $this->discount;
    }

    /**
     * Get the instance as an array.
     *
     * @return array
     */
    public function toArray()
    {
        return $this->asStripeDiscount()->toArray();
    }

    /**
     * Convert the object to its JSON representation.
     *
     * @param  int  $options
     * @return string
     */
    public function toJson($options = 0)
    {
        return json_encode($this->jsonSerialize(), $options);
    }

    /**
     * Convert the object into something JSON serializable.
     *
     * @return array
     */
    #[\ReturnTypeWillChange]
    public function jsonSerialize()
    {
        return $this->toArray();
    }

    /**
     * Dynamically get values from the Stripe object.
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get(string $key)
    {
        return $this->discount->{$key};
    }
}
