<?php

    namespace Modules\Lpj\Services;

    use Illuminate\Database\Eloquent\Collection;
    use Illuminate\Pagination\LengthAwarePaginator;
    use Modules\Lpj\Models\BankData;

    class BankDataService
    {
        protected $bankData;

        public function __construct(BankData $bankData)
        {
            $this->bankData = $bankData;
        }

        /**
         * Get all bank data with optional filtering and pagination
         *
         * @param array    $filters
         * @param int|null $perPage
         *
         * @return Collection|LengthAwarePaginator
         */
        public function getAllBankData(array $filters = [], ?int $perPage = null)
        {
            $query = $this->bankData->newQuery();

            // Apply filters
            if (isset($filters['village_code'])) {
                $query->ofVillage($filters['village_code']);
            }
            if (isset($filters['district_code'])) {
                $query->ofDistrict($filters['district_code']);
            }
            if (isset($filters['city_code'])) {
                $query->ofCity($filters['city_code']);
            }
            if (isset($filters['province_code'])) {
                $query->ofProvince($filters['province_code']);
            }
            if (isset($filters['date'])) {
                $query->ofDate($filters['date']);
            }
            if (isset($filters['start_date']) && isset($filters['end_date'])) {
                $query->betweenDates($filters['start_date'], $filters['end_date']);
            }
            if (isset($filters['year'])) {
                $query->ofYear($filters['year']);
            }
            if (isset($filters['asset_type'])) {
                $query->ofAssetType($filters['asset_type']);
            }

            // Add more filters as needed

            return $perPage ? $query->paginate($perPage) : $query->get();
        }

        /**
         * Create a new bank data entry
         *
         * @param array $data
         *
         * @return BankData
         */
        public function createBankData(array $data)
        : BankData
        {
            return $this->bankData->create($data);
        }

        /**
         * Update an existing bank data entry
         *
         * @param int   $id
         * @param array $data
         *
         * @return BankData
         */
        public function updateBankData(int $id, array $data)
        : BankData
        {
            $bankData = $this->bankData->findOrFail($id);
            $bankData->update($data);
            return $bankData;
        }

        /**
         * Delete a bank data entry
         *
         * @param int $id
         *
         * @return bool
         */
        public function deleteBankData(int $id)
        : bool
        {
            $bankData = $this->bankData->findOrFail($id);
            return $bankData->delete();
        }

        /**
         * Find a bank data entry by ID
         *
         * @param int $id
         *
         * @return BankData|null
         */
        public function findBankData(int $id)
        : ?BankData
        {
            return $this->bankData->find($id);
        }
    }
