<?php

namespace App\Repositories;

use App\Models\Booking;

class BookingRepository extends BaseRepository
{
    protected $model;

    public function __construct()
    {
        $this->model = new Booking();
    }

    public function getByCampaignAndCreator(string $campaignId, string $creatorId, ?string $orgHex, ?string $type = null)
    {
        $campaignBin = $this->normalizeId($campaignId);
        $creatorBin = $this->normalizeId($creatorId);
        $query = $this->model->where('campaign_id', $campaignBin)
            ->where('creator_id', $creatorBin);
        if ($type !== null) {
            $query->where('type', $type);
        }
        return $this->applyOrganizationScope($query, $orgHex)->first();
    }

    public function getByIdInOrganization(string $id, ?string $orgHex)
    {
        $normalizedId = $this->normalizeId($id);
        $query = $this->model->where('id', $normalizedId);
        return $this->applyOrganizationScope($query, $orgHex)->first();
    }

    public function getWithRelationsInOrganization(string $id, ?string $orgHex)
    {
        $normalizedId = $this->normalizeId($id);
        $query = $this->model->with(['campaign', 'creator', 'videos', 'assignedTo', 'bookingVideo', 'bookingLivestream'])->where('id', $normalizedId);
        return $this->applyOrganizationScope($query, $orgHex)->first();
    }

    public function create(array $data)
    {
        return $this->model->create($data);
    }

    public function updateInOrganization(string $id, array $data, ?string $orgHex)
    {
        $normalizedId = $this->normalizeId($id);
        $query = $this->model->where('id', $normalizedId);
        $booking = $this->applyOrganizationScope($query, $orgHex)->first();

        if ($booking) {
            $booking->update($data);
        }

        return $booking;
    }

    public function deleteInOrganization(string $id, ?string $orgHex)
    {
        $normalizedId = $this->normalizeId($id);
        $query = $this->model->where('id', $normalizedId);
        return $this->applyOrganizationScope($query, $orgHex)->delete();
    }

    public function paginateByOrganization(?string $orgHex, int $perPage = 15)
    {
        $query = $this->model->query();
        $query = $this->applyOrganizationScope($query, $orgHex);
        return $query->paginate($perPage);
    }

    /**
     * Lấy booking đủ điều kiện cho trang quản lý video.
     * Nếu skip/limit = null thì trả về tất cả (dùng cho metrics).
     */
    public function getEligibleForVideoManagement(?string $orgHex, ?int $skip = null, ?int $limit = null)
    {
        $query = $this->model->query()
            ->with(['campaign', 'creator', 'videos', 'assignedTo', 'bookingVideo'])
            ->where('status', \App\Enums\BookingStatus::COMPLETED->value)
            ->where(function ($q) {
                $q->whereHas('bookingVideo', function ($q) {
                    $q->where('number_of_videos', '>', 0);
                })->orHas('videos');
            });

        $query = $this->applyOrganizationScope($query, $orgHex);

        $query->orderByDesc('created_at');

        if ($skip !== null) {
            $query->skip($skip);
        }
        if ($limit !== null) {
            $query->take($limit);
        }

        return $query->get();
    }
}
