<?php

namespace Database\Seeders;

use App\Models\Menu;
use App\Models\Page;
use App\Models\Plan;
use App\Models\Post;
use App\Models\Setting;
use App\Models\Testimonial;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // ------------------------------------------------ Admin user
        User::updateOrCreate(
            ['email' => 'admin@farmpilot.io'],
            [
                'name' => 'FarmPilot Admin',
                'password' => Hash::make('ChangeMe!2026'),
                'is_admin' => true,
            ],
        );

        // ------------------------------------------------ Settings
        $settings = [
            'site_name' => 'FarmPilot',
            'tagline' => 'Run your whole farm from one dashboard',
            'meta_title' => 'FarmPilot — Farm automation without the fragmentation',
            'meta_description' => 'FarmPilot connects diesel pumps, tanks, bores, weather stations, solar and soil probes into one dashboard with plain-English automation. Join early access.',
            'hero_headline' => 'Every pump, tank and bore. One dashboard.',
            'hero_subline' => 'FarmPilot connects the gear you already own — diesel pumps, tanks, bores, weather stations, solar — and lets you automate it in plain English. No more driving 40 minutes to flick a switch.',
            'coming_soon' => true,
            'newsletter_double_opt_in' => true,
            'contact_email' => 'hello@farmpilot.io',
            'footer_text' => 'One dashboard for every pump, tank, bore and sensor on the place. Hardware-agnostic, built for Australian conditions.',
            'home_features' => [
                [
                    'tag' => 'HARDWARE-AGNOSTIC',
                    'title' => 'Bring the gear you already own',
                    'description' => 'Homemade pump controller? Ten-year-old weather station? If it speaks Modbus, MQTT, HTTP or plain relay logic, FarmPilot can drive it. No rip-and-replace.',
                ],
                [
                    'tag' => 'AUTOMATION',
                    'title' => 'Rules in plain English',
                    'description' => 'IF tank below 40% AND no rain forecast, THEN start the pump and text Dave. Build SCADA-grade automation without the SCADA-grade consultant.',
                ],
                [
                    'tag' => 'ALERTS',
                    'title' => 'Know before it becomes a drive',
                    'description' => 'Low oil pressure, dropping tank levels, a generator that failed to start — get an SMS or push alert the moment it happens, not when you notice the trough is dry.',
                ],
                [
                    'tag' => 'OFFLINE-FIRST',
                    'title' => 'Keeps working when the internet does not',
                    'description' => 'The FarmPilot gateway caches data and keeps running your automation rules locally through outages, then syncs the moment the connection comes back.',
                ],
                [
                    'tag' => 'ONE LOGIN',
                    'title' => 'Every device, one place',
                    'description' => 'Pumps, bores, tanks, solar, cameras, soil probes — stop juggling six vendor apps with six logins. One dashboard, one history, one set of alerts.',
                ],
                [
                    'tag' => 'MULTI-FARM',
                    'title' => 'Built for teams and contractors',
                    'description' => 'Multiple farms, user permissions, installer access and audit logs. Your irrigation contractor commissions their system without seeing anyone else\'s.',
                ],
            ],
        ];

        foreach ($settings as $key => $value) {
            if (Setting::get($key) === null) {
                Setting::set($key, $value);
            }
        }

        // ------------------------------------------------ Pages
        $about = Page::firstOrCreate(['slug' => 'about'], [
            'title' => 'About FarmPilot',
            'meta_description' => 'Why FarmPilot exists: farm tech is fragmented, and farmers deserve one dashboard for everything.',
            'body' => <<<'MD'
The biggest problem in farm tech isn't a missing sensor — it's fragmentation. One farm might run a diesel pump with a homemade controller, a weather station from one manufacturer, soil probes from another, tanks with float switches, a bore controller, solar inverters and a couple of cameras. Each with its own app, login and interface — if it has one at all.

**FarmPilot is the place where all of it connects.**

We're building a hardware-agnostic agricultural automation platform: devices, capabilities and plain-English automation rules instead of vendor lock-in. A pump is just one type of device.

## What we believe

- You shouldn't have to replace working gear to get a modern dashboard.
- Automation should read like a sentence, not a PLC program.
- Pricing should be flat and predictable — never per API call.
- The platform stays open: we publish the API so anyone can integrate their own hardware.

FarmPilot is currently in pre-launch. [Join the early access list](/#waitlist) to get in first and lock launch pricing for life.
MD,
        ]);

        $contact = Page::firstOrCreate(['slug' => 'contact'], [
            'title' => 'Contact',
            'meta_description' => 'Get in touch with the FarmPilot team.',
            'body' => "Questions, hardware you want supported, or a farm that would make a good pilot site?\n\nEmail us at **hello@farmpilot.io** — we read everything.",
        ]);

        $privacy = Page::firstOrCreate(['slug' => 'privacy'], [
            'title' => 'Privacy Policy',
            'published' => true,
            'body' => "_Draft — replace with your final policy before launch._\n\nWe collect the email address you give us to send launch updates and, if you buy from the store, the details needed to fulfil your order. We never sell your data.",
        ]);

        // ------------------------------------------------ Menus
        $header = Menu::firstOrCreate(['location' => 'header'], ['name' => 'Header']);
        if ($header->items()->count() === 0) {
            $header->items()->createMany([
                ['label' => 'Features', 'url' => '/#features', 'sort' => 1],
                ['label' => 'Pricing', 'url' => '/plans', 'sort' => 2],
                ['label' => 'Store', 'url' => '/store', 'sort' => 3],
                ['label' => 'Blog', 'url' => '/blog', 'sort' => 4],
                ['label' => 'About', 'page_id' => $about->id, 'sort' => 5],
            ]);
        }

        $footer = Menu::firstOrCreate(['location' => 'footer'], ['name' => 'Footer']);
        if ($footer->items()->count() === 0) {
            $footer->items()->createMany([
                ['label' => 'About', 'page_id' => $about->id, 'sort' => 1],
                ['label' => 'Blog', 'url' => '/blog', 'sort' => 2],
                ['label' => 'Plans', 'url' => '/plans', 'sort' => 3],
                ['label' => 'Store', 'url' => '/store', 'sort' => 4],
                ['label' => 'Contact', 'page_id' => $contact->id, 'sort' => 5],
                ['label' => 'Privacy', 'page_id' => $privacy->id, 'sort' => 6],
            ]);
        }

        // ------------------------------------------------ Testimonials
        if (Testimonial::count() === 0) {
            Testimonial::insert([
                [
                    'name' => 'Greg M.',
                    'role_company' => 'Mixed cropping, Riverina NSW',
                    'quote' => 'The north bore is a 40-minute round trip. Being able to start the pump from the kitchen table before smoko has already paid for itself.',
                    'published' => true, 'sort' => 1, 'created_at' => now(), 'updated_at' => now(),
                ],
                [
                    'name' => 'Sarah T.',
                    'role_company' => 'Beef, Central QLD',
                    'quote' => 'We had four different apps for tanks, weather, solar and the genset. Now it\'s one screen, and the low-tank SMS has saved a trough twice this summer.',
                    'published' => true, 'sort' => 2, 'created_at' => now(), 'updated_at' => now(),
                ],
                [
                    'name' => 'Dave K.',
                    'role_company' => 'Irrigation contractor, Goulburn Valley VIC',
                    'quote' => 'I can commission a customer\'s system remotely without touching anyone else\'s farm. The installer access is exactly how it should work.',
                    'published' => true, 'sort' => 3, 'created_at' => now(), 'updated_at' => now(),
                ],
            ]);
        }

        // ------------------------------------------------ Plans (Stripe syncs on save if keys are set)
        if (Plan::count() === 0) {
            $plans = [
                [
                    'name' => 'Free', 'slug' => 'free',
                    'description' => 'Kick the tyres with your first devices.',
                    'price_cents' => 0, 'interval' => 'month', 'sort' => 1,
                    'features' => ['5 devices', 'Live dashboards', 'Manual control', '7-day history'],
                ],
                [
                    'name' => 'Farm', 'slug' => 'farm',
                    'description' => 'The whole place, automated.',
                    'price_cents' => 4900, 'interval' => 'month', 'trial_days' => 14,
                    'highlighted' => true, 'sort' => 2,
                    'features' => ['25 devices', 'Plain-English automation rules', 'SMS + push alerts', '12-month history', 'Weather integration'],
                ],
                [
                    'name' => 'Professional', 'slug' => 'professional',
                    'description' => 'Multi-farm operations and agronomists.',
                    'price_cents' => 14900, 'interval' => 'month', 'trial_days' => 14, 'sort' => 3,
                    'features' => ['Unlimited devices', 'Multi-farm management', 'User permissions & contractor access', 'Reporting & exports', 'API access'],
                ],
                [
                    'name' => 'Enterprise', 'slug' => 'enterprise',
                    'description' => 'Dealers, installers and white-label.',
                    'price_cents' => 49900, 'interval' => 'month', 'sort' => 4,
                    'features' => ['Everything in Professional', 'White-labelling', 'Dealer management', 'Audit logs', 'Priority support'],
                ],
            ];

            foreach ($plans as $plan) {
                Plan::create($plan);
            }
        }

        // ------------------------------------------------ Launch post
        Post::firstOrCreate(['slug' => 'why-we-are-building-farmpilot'], [
            'title' => "Why we're building FarmPilot",
            'excerpt' => 'Farm tech is fragmented. Six devices, six apps, six logins. We think a farm deserves one dashboard — here\'s the plan.',
            'published_at' => now(),
            'body' => <<<'MD'
It started as a way to switch a diesel pump on remotely. It stopped being that the moment we counted the apps on one farmer's phone: a weather station app, a tank monitor app, a solar inverter app, a camera app, and a bore controller with no app at all — just a padlocked switchboard 40 minutes from the house.

## The real problem is fragmentation

Every manufacturer ships its own silo. None of them talk to each other, and none of them let you write the one rule every farmer actually wants:

> **IF** the tank drops below 40% **AND** tomorrow won't rain, **THEN** start the pump — and text me if it doesn't.

## What FarmPilot does differently

FarmPilot is hardware-agnostic. Devices expose *capabilities* — start, stop, level, RPM, rainfall, watts — and the dashboard builds itself from what your gear can do. A lightweight gateway on the farm translates Modbus, MQTT, HTTP and plain relay I/O into one secure connection, and keeps your automation running even when the internet drops.

We're opening early access soon. [Join the list](/#waitlist) and you'll be first through the gate — with launch pricing locked for life.
MD,
        ]);
    }
}
