<?php

namespace Database\Seeders;

use App\Models\AllowedAdAccount;
use Illuminate\Database\Seeder;

class MetaAdAccountSeeder extends Seeder
{
    public function run(): void
    {
        $accounts = [
            ['name' => 'BING-KnowledgeDomain-NEW-OBM', 'account_id' => '3010881492442898', 'is_active' => true],
            ['name' => 'MidoWeb-WWBlog-GMT-RSOC', 'account_id' => '2220667645134722', 'is_active' => true],
            ['name' => 'Mohsin Khan', 'account_id' => '463064214443673', 'is_active' => true],
            ['name' => 'CC - AD MEDIA 2', 'account_id' => '824024712556945', 'is_active' => false],
            ['name' => 'Amazon AdMedia', 'account_id' => '1495918977704993', 'is_active' => false],
            ['name' => 'Direct Ads AdMedia', 'account_id' => '813970954278439', 'is_active' => true],
            ['name' => 'BK - AdMED 1', 'account_id' => '827677745418699', 'is_active' => false],
            ['name' => 'Buzzour AdMedia', 'account_id' => '534738702399752', 'is_active' => true],
            ['name' => 'Technorati1', 'account_id' => '451037767675415', 'is_active' => true],
            ['name' => 'Yahoo KD', 'account_id' => '1899605027230860', 'is_active' => true],
            ['name' => 'Hearst-MediaBuys', 'account_id' => '602749402192836', 'is_active' => true],
            ['name' => 'Yahoo AM', 'account_id' => '1244876930069625', 'is_active' => true],
            ['name' => 'AFS AdMedia', 'account_id' => '1060186255550793', 'is_active' => true],
            ['name' => 'Yahoo BS', 'account_id' => '1628416691046178', 'is_active' => true],
            ['name' => 'Blueto', 'account_id' => '545233394560445', 'is_active' => true],
            ['name' => 'Yahoo Ads', 'account_id' => '580820141014275', 'is_active' => true],
            ['name' => 'KR Yahoo Ads', 'account_id' => '1610763393153974', 'is_active' => true],
            ['name' => 'Connexity_Buy_Roamans', 'account_id' => '1853393292161342', 'is_active' => true],
            ['name' => 'Yahoo Ad MK', 'account_id' => '450715341409201', 'is_active' => true],
            ['name' => 'SearchGPT', 'account_id' => '1184109403257239', 'is_active' => true],
            ['name' => 'BridalFusion-BING', 'account_id' => '9151285488315206', 'is_active' => true],
            ['name' => 'Rebates.com', 'account_id' => '1360680365071552', 'is_active' => true],
            ['name' => 'ReviewTravel', 'account_id' => '731596952735979', 'is_active' => true],
            ['name' => 'Dealtimes', 'account_id' => '1034722795450623', 'is_active' => true],
            ['name' => 'EconomicChart', 'account_id' => '1647426089265986', 'is_active' => true],
            ['name' => 'AutoAutomotive', 'account_id' => '1238155271384134', 'is_active' => true],
            ['name' => 'LifeInterest', 'account_id' => '1777671242962048', 'is_active' => true],
            ['name' => 'TalkGPT', 'account_id' => '1950320145707342', 'is_active' => true],
            ['name' => 'savepages-amazon-arb', 'account_id' => '4398382103816289', 'is_active' => true],
            ['name' => 'New-Dealtimes', 'account_id' => '1036440258421094', 'is_active' => true],
            ['name' => 'RSOC-EasyAutos', 'account_id' => '1906028840268550', 'is_active' => true],
            ['name' => 'Bing-MapMyCity', 'account_id' => '855776730732623', 'is_active' => true],
            ['name' => 'Bing-TheAnswerHub-OBMEDIA', 'account_id' => '25021599314180332', 'is_active' => true],
            ['name' => 'RSOC-Classified', 'account_id' => '899032093285744', 'is_active' => true],
            ['name' => 'Bing-CompareBox', 'account_id' => '2304851260011575', 'is_active' => true],
            ['name' => 'Bing-Knowledge-Domain-OBMEDIA', 'account_id' => '1640385486931408', 'is_active' => true],
            ['name' => 'Influential-TheAgency', 'account_id' => '667436803095499', 'is_active' => true],
            ['name' => 'RSOC-QueryPilot-Pred', 'account_id' => '1828214511070310', 'is_active' => true],
            ['name' => 'Store', 'account_id' => '1845701776070630', 'is_active' => true],
            ['name' => 'DB-Store.com', 'account_id' => '1505359920930212', 'is_active' => true],
            ['name' => 'Bing-SeekOmni-OBMEDIA', 'account_id' => '945814791158486', 'is_active' => true],
            ['name' => 'StockPrices', 'account_id' => '1418821929380936', 'is_active' => true],
            ['name' => 'Leisure.com', 'account_id' => '26632801489687888', 'is_active' => true],
            ['name' => 'iblockads', 'account_id' => '1334986461919543', 'is_active' => true],
        ];

        foreach ($accounts as $account) {
            // is_active is set when the row is created and never rewritten.
            //
            // ProductionSeeder runs on every release, so seeding it on update
            // reverted the operators. is_active is the spend guardrail -
            // allowedIds() filters on it and assertAccountAllowed() refuses
            // anything not in that set - and nothing in the application writes
            // it, so SQL is the only way to revoke an account. A compromised or
            // overspending account disabled by hand came back on the next
            // deploy with nothing in the output to say so.
            $row = AllowedAdAccount::firstOrNew(
                ['platform' => 'meta', 'account_id' => AllowedAdAccount::normalizeAccountId($account['account_id'])],
            );

            $row->name = $account['name'];

            if (! $row->exists) {
                $row->is_active = $account['is_active'];
            }

            $row->save();
        }
    }
}
