<?php

namespace app\service;

use GuzzleHttp\Client;

class MetaBusinessService
{
    protected $client;
    protected $accessToken;
    protected $businessId;

    public function __construct()
    {
        $this->client = new Client();
        $this->accessToken = getenv('META_ACCESS_TOKEN');
        $this->businessId = getenv('META_BUSINESS_ID');
    }

    // 获取广告账户
    public function getBusinessAccounts()
    {
        $url = "https://graph.facebook.com/v15.0/{$this->businessId}/owned_ad_accounts";
        $response = $this->client->get($url, [
            'query' => [
                'access_token' => $this->accessToken,
            ]
        ]);

        return json_decode($response->getBody()->getContents(), true)['data'] ?? [];
    }

    // 保存广告账户到数据库
    public function saveBusinessAccounts($accounts)
    {
        foreach ($accounts as $account) {
            // 假设你已经定义了 MetaBusinessAccount 模型
            MetaBusinessAccount::create([
                'account_name' => $account['name'],
                'account_id' => $account['id'],
                'status' => $account['status'],
            ]);
        }
    }

    // 获取广告系列
    public function getCampaigns()
    {
        $url = "https://graph.facebook.com/v15.0/{$this->businessId}/campaigns";
        $response = $this->client->get($url, [
            'query' => [
                'access_token' => $this->accessToken,
            ]
        ]);

        return json_decode($response->getBody()->getContents(), true)['data'] ?? [];
    }

    // 创建广告系列
    public function createCampaign($name, $objective, $status)
    {
        $url = "https://graph.facebook.com/v15.0/{$this->businessId}/campaigns";
        $response = $this->client->post($url, [
            'json' => [
                'name' => $name,
                'objective' => $objective,
                'status' => $status,
                'access_token' => $this->accessToken,
            ]
        ]);

        return json_decode($response->getBody()->getContents(), true);
    }

    // 获取广告组
    public function getAdGroups($campaignId)
    {
        $url = "https://graph.facebook.com/v15.0/{$campaignId}/adsets";
        $response = $this->client->get($url, [
            'query' => [
                'access_token' => $this->accessToken,
            ]
        ]);

        return json_decode($response->getBody()->getContents(), true)['data'] ?? [];
    }

    // 创建广告组
    public function createAdGroup($campaignId, $name, $status)
    {
        $url = "https://graph.facebook.com/v15.0/{$this->businessId}/adsets";
        $response = $this->client->post($url, [
            'json' => [
                'name' => $name,
                'status' => $status,
                'campaign_id' => $campaignId,
                'access_token' => $this->accessToken,
            ]
        ]);

        return json_decode($response->getBody()->getContents(), true);
    }

    // 获取广告
    public function getAds($adGroupId)
    {
        $url = "https://graph.facebook.com/v15.0/{$adGroupId}/ads";
        $response = $this->client->get($url, [
            'query' => [
                'access_token' => $this->accessToken,
            ]
        ]);

        return json_decode($response->getBody()->getContents(), true)['data'] ?? [];
    }

    // 创建广告
    public function createAd($adGroupId, $name, $status)
    {
        $url = "https://graph.facebook.com/v15.0/{$this->businessId}/ads";
        $response = $this->client->post($url, [
            'json' => [
                'name' => $name,
                'status' => $status,
                'adset_id' => $adGroupId,
                'access_token' => $this->accessToken,
            ]
        ]);

        return json_decode($response->getBody()->getContents(), true);
    }
}